2015-02-24 21:05:19 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2020-04-29 12:57:22 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2015-04-07 18:02:49 +03:00
|
|
|
* @author Clark Tomlinson <fallen013@gmail.com>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2020-08-24 15:54:25 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Stefan Weiberg <sweiberg@suse.com>
|
2015-04-07 18:02:49 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
2015-02-24 21:05:19 +03:00
|
|
|
* @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,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-02-24 21:05:19 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Encryption\Crypto;
|
|
|
|
|
|
|
|
use OC\Encryption\Exceptions\DecryptionFailedException;
|
|
|
|
use OC\Encryption\Exceptions\EncryptionFailedException;
|
2020-08-12 14:22:24 +03:00
|
|
|
use OC\ServerNotAvailableException;
|
2015-04-01 14:58:23 +03:00
|
|
|
use OCA\Encryption\Exceptions\MultiKeyDecryptException;
|
|
|
|
use OCA\Encryption\Exceptions\MultiKeyEncryptException;
|
2015-04-01 17:36:08 +03:00
|
|
|
use OCP\Encryption\Exceptions\GenericEncryptionException;
|
2015-02-24 21:05:19 +03:00
|
|
|
use OCP\IConfig;
|
2016-01-05 14:51:05 +03:00
|
|
|
use OCP\IL10N;
|
2015-02-24 21:05:19 +03:00
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
|
2016-01-04 23:00:55 +03:00
|
|
|
/**
|
2017-04-12 07:16:27 +03:00
|
|
|
* Class Crypt provides the encryption implementation of the default Nextcloud
|
2016-01-04 23:00:55 +03:00
|
|
|
* encryption module. As default AES-256-CTR is used, it does however offer support
|
|
|
|
* for the following modes:
|
|
|
|
*
|
|
|
|
* - AES-256-CTR
|
|
|
|
* - AES-128-CTR
|
|
|
|
* - AES-256-CFB
|
|
|
|
* - AES-128-CFB
|
|
|
|
*
|
2016-01-08 20:12:22 +03:00
|
|
|
* For integrity protection Encrypt-Then-MAC using HMAC-SHA256 is used.
|
|
|
|
*
|
2016-01-04 23:00:55 +03:00
|
|
|
* @package OCA\Encryption\Crypto
|
|
|
|
*/
|
2015-02-24 21:05:19 +03:00
|
|
|
class Crypt {
|
2020-04-10 17:54:27 +03:00
|
|
|
public const DEFAULT_CIPHER = 'AES-256-CTR';
|
2017-04-12 07:16:27 +03:00
|
|
|
// default cipher from old Nextcloud versions
|
2020-04-10 17:54:27 +03:00
|
|
|
public const LEGACY_CIPHER = 'AES-128-CFB';
|
2015-02-24 21:05:19 +03:00
|
|
|
|
2017-04-12 07:16:27 +03:00
|
|
|
// default key format, old Nextcloud version encrypted the private key directly
|
2015-08-07 15:04:17 +03:00
|
|
|
// with the user password
|
2020-04-10 17:54:27 +03:00
|
|
|
public const LEGACY_KEY_FORMAT = 'password';
|
2015-08-07 15:04:17 +03:00
|
|
|
|
2020-04-10 17:54:27 +03:00
|
|
|
public const HEADER_START = 'HBEGIN';
|
|
|
|
public const HEADER_END = 'HEND';
|
2016-01-05 14:51:05 +03:00
|
|
|
|
2016-01-04 23:00:55 +03:00
|
|
|
/** @var ILogger */
|
2015-02-24 21:05:19 +03:00
|
|
|
private $logger;
|
2016-01-05 14:51:05 +03:00
|
|
|
|
2016-01-04 23:00:55 +03:00
|
|
|
/** @var string */
|
2015-02-24 21:05:19 +03:00
|
|
|
private $user;
|
2016-01-05 14:51:05 +03:00
|
|
|
|
2016-01-04 23:00:55 +03:00
|
|
|
/** @var IConfig */
|
2015-02-24 21:05:19 +03:00
|
|
|
private $config;
|
2016-01-05 14:51:05 +03:00
|
|
|
|
2016-01-04 23:00:55 +03:00
|
|
|
/** @var array */
|
2015-08-07 15:04:17 +03:00
|
|
|
private $supportedKeyFormats;
|
2016-01-05 14:51:05 +03:00
|
|
|
|
|
|
|
/** @var IL10N */
|
|
|
|
private $l;
|
|
|
|
|
2016-01-04 23:00:55 +03:00
|
|
|
/** @var array */
|
|
|
|
private $supportedCiphersAndKeySize = [
|
|
|
|
'AES-256-CTR' => 32,
|
|
|
|
'AES-128-CTR' => 16,
|
|
|
|
'AES-256-CFB' => 32,
|
|
|
|
'AES-128-CFB' => 16,
|
|
|
|
];
|
2015-08-07 15:04:17 +03:00
|
|
|
|
2020-08-12 14:22:24 +03:00
|
|
|
/** @var bool */
|
|
|
|
private $supportLegacy;
|
|
|
|
|
2015-02-24 21:05:19 +03:00
|
|
|
/**
|
|
|
|
* @param ILogger $logger
|
|
|
|
* @param IUserSession $userSession
|
|
|
|
* @param IConfig $config
|
2016-01-05 14:51:05 +03:00
|
|
|
* @param IL10N $l
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
2016-01-05 14:51:05 +03:00
|
|
|
public function __construct(ILogger $logger, IUserSession $userSession, IConfig $config, IL10N $l) {
|
2015-02-24 21:05:19 +03:00
|
|
|
$this->logger = $logger;
|
2015-10-22 12:46:37 +03:00
|
|
|
$this->user = $userSession && $userSession->isLoggedIn() ? $userSession->getUser()->getUID() : '"no user given"';
|
2015-02-24 21:05:19 +03:00
|
|
|
$this->config = $config;
|
2016-01-05 14:51:05 +03:00
|
|
|
$this->l = $l;
|
2015-08-07 15:04:17 +03:00
|
|
|
$this->supportedKeyFormats = ['hash', 'password'];
|
2020-08-12 14:22:24 +03:00
|
|
|
|
|
|
|
$this->supportLegacy = $this->config->getSystemValueBool('encryption.legacy_format_support', false);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-09 15:59:03 +03:00
|
|
|
* create new private/public key-pair for user
|
|
|
|
*
|
2015-04-02 18:53:40 +03:00
|
|
|
* @return array|bool
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
|
|
|
public function createKeyPair() {
|
|
|
|
$log = $this->logger;
|
|
|
|
$res = $this->getOpenSSLPKey();
|
|
|
|
|
|
|
|
if (!$res) {
|
2015-10-22 12:46:37 +03:00
|
|
|
$log->error("Encryption Library couldn't generate users key-pair for {$this->user}",
|
2015-03-25 00:29:10 +03:00
|
|
|
['app' => 'encryption']);
|
2015-02-24 21:05:19 +03:00
|
|
|
|
|
|
|
if (openssl_error_string()) {
|
2015-03-25 00:29:10 +03:00
|
|
|
$log->error('Encryption library openssl_pkey_new() fails: ' . openssl_error_string(),
|
|
|
|
['app' => 'encryption']);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
2015-03-25 00:29:10 +03:00
|
|
|
} elseif (openssl_pkey_export($res,
|
|
|
|
$privateKey,
|
|
|
|
null,
|
|
|
|
$this->getOpenSSLConfig())) {
|
2015-02-24 21:05:19 +03:00
|
|
|
$keyDetails = openssl_pkey_get_details($res);
|
|
|
|
$publicKey = $keyDetails['key'];
|
|
|
|
|
|
|
|
return [
|
|
|
|
'publicKey' => $publicKey,
|
|
|
|
'privateKey' => $privateKey
|
|
|
|
];
|
|
|
|
}
|
2015-10-22 12:46:37 +03:00
|
|
|
$log->error('Encryption library couldn\'t export users private key, please check your servers OpenSSL configuration.' . $this->user,
|
2015-03-25 00:29:10 +03:00
|
|
|
['app' => 'encryption']);
|
2015-02-24 21:05:19 +03:00
|
|
|
if (openssl_error_string()) {
|
2015-03-25 00:29:10 +03:00
|
|
|
$log->error('Encryption Library:' . openssl_error_string(),
|
|
|
|
['app' => 'encryption']);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-09 15:59:03 +03:00
|
|
|
* Generates a new private key
|
|
|
|
*
|
2015-02-24 21:05:19 +03:00
|
|
|
* @return resource
|
|
|
|
*/
|
|
|
|
public function getOpenSSLPKey() {
|
|
|
|
$config = $this->getOpenSSLConfig();
|
|
|
|
return openssl_pkey_new($config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-09 15:59:03 +03:00
|
|
|
* get openSSL Config
|
|
|
|
*
|
2015-02-24 21:05:19 +03:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getOpenSSLConfig() {
|
|
|
|
$config = ['private_key_bits' => 4096];
|
2015-04-09 15:59:03 +03:00
|
|
|
$config = array_merge(
|
|
|
|
$config,
|
|
|
|
$this->config->getSystemValue('openssl', [])
|
|
|
|
);
|
2015-02-24 21:05:19 +03:00
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-02 18:53:40 +03:00
|
|
|
* @param string $plainContent
|
|
|
|
* @param string $passPhrase
|
2016-02-02 22:00:36 +03:00
|
|
|
* @param int $version
|
2016-02-03 16:32:04 +03:00
|
|
|
* @param int $position
|
2015-11-27 18:32:44 +03:00
|
|
|
* @return false|string
|
2016-01-05 01:06:23 +03:00
|
|
|
* @throws EncryptionFailedException
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
2016-02-03 16:32:04 +03:00
|
|
|
public function symmetricEncryptFileContent($plainContent, $passPhrase, $version, $position) {
|
2015-02-24 21:05:19 +03:00
|
|
|
if (!$plainContent) {
|
2015-03-25 00:29:10 +03:00
|
|
|
$this->logger->error('Encryption Library, symmetrical encryption failed no content given',
|
|
|
|
['app' => 'encryption']);
|
2015-02-24 21:05:19 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$iv = $this->generateIv();
|
|
|
|
|
2015-04-01 14:58:23 +03:00
|
|
|
$encryptedContent = $this->encrypt($plainContent,
|
|
|
|
$iv,
|
|
|
|
$passPhrase,
|
|
|
|
$this->getCipher());
|
2016-01-05 14:51:05 +03:00
|
|
|
|
2016-02-02 22:00:36 +03:00
|
|
|
// Create a signature based on the key as well as the current version
|
2020-08-11 20:26:41 +03:00
|
|
|
$sig = $this->createSignature($encryptedContent, $passPhrase.'_'.$version.'_'.$position);
|
2016-01-05 14:51:05 +03:00
|
|
|
|
2015-04-01 14:58:23 +03:00
|
|
|
// combine content to encrypt the IV identifier and actual IV
|
|
|
|
$catFile = $this->concatIV($encryptedContent, $iv);
|
2016-01-05 14:51:05 +03:00
|
|
|
$catFile = $this->concatSig($catFile, $sig);
|
2018-02-13 23:55:24 +03:00
|
|
|
return $this->addPadding($catFile);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
2015-04-08 15:19:11 +03:00
|
|
|
/**
|
|
|
|
* generate header for encrypted file
|
2015-08-07 15:04:17 +03:00
|
|
|
*
|
|
|
|
* @param string $keyFormat (can be 'hash' or 'password')
|
|
|
|
* @return string
|
|
|
|
* @throws \InvalidArgumentException
|
2015-04-08 15:19:11 +03:00
|
|
|
*/
|
2015-08-07 15:04:17 +03:00
|
|
|
public function generateHeader($keyFormat = 'hash') {
|
|
|
|
if (in_array($keyFormat, $this->supportedKeyFormats, true) === false) {
|
|
|
|
throw new \InvalidArgumentException('key format "' . $keyFormat . '" is not supported');
|
|
|
|
}
|
|
|
|
|
2015-04-08 15:19:11 +03:00
|
|
|
$cipher = $this->getCipher();
|
2015-08-07 15:04:17 +03:00
|
|
|
|
|
|
|
$header = self::HEADER_START
|
|
|
|
. ':cipher:' . $cipher
|
|
|
|
. ':keyFormat:' . $keyFormat
|
|
|
|
. ':' . self::HEADER_END;
|
2015-04-08 15:19:11 +03:00
|
|
|
|
|
|
|
return $header;
|
|
|
|
}
|
|
|
|
|
2015-02-24 21:05:19 +03:00
|
|
|
/**
|
2015-04-02 18:53:40 +03:00
|
|
|
* @param string $plainContent
|
|
|
|
* @param string $iv
|
2015-04-01 14:58:23 +03:00
|
|
|
* @param string $passPhrase
|
2015-02-24 21:05:19 +03:00
|
|
|
* @param string $cipher
|
|
|
|
* @return string
|
|
|
|
* @throws EncryptionFailedException
|
|
|
|
*/
|
2015-04-01 14:58:23 +03:00
|
|
|
private function encrypt($plainContent, $iv, $passPhrase = '', $cipher = self::DEFAULT_CIPHER) {
|
2015-03-25 00:29:10 +03:00
|
|
|
$encryptedContent = openssl_encrypt($plainContent,
|
|
|
|
$cipher,
|
2015-04-01 14:58:23 +03:00
|
|
|
$passPhrase,
|
2015-03-25 00:29:10 +03:00
|
|
|
false,
|
|
|
|
$iv);
|
2015-02-24 21:05:19 +03:00
|
|
|
|
|
|
|
if (!$encryptedContent) {
|
|
|
|
$error = 'Encryption (symmetric) of content failed';
|
2015-03-25 00:29:10 +03:00
|
|
|
$this->logger->error($error . openssl_error_string(),
|
|
|
|
['app' => 'encryption']);
|
2015-02-24 21:05:19 +03:00
|
|
|
throw new EncryptionFailedException($error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $encryptedContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-09 15:59:03 +03:00
|
|
|
* return Cipher either from config.php or the default cipher defined in
|
|
|
|
* this class
|
|
|
|
*
|
|
|
|
* @return string
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
|
|
|
public function getCipher() {
|
|
|
|
$cipher = $this->config->getSystemValue('cipher', self::DEFAULT_CIPHER);
|
2016-01-04 23:00:55 +03:00
|
|
|
if (!isset($this->supportedCiphersAndKeySize[$cipher])) {
|
|
|
|
$this->logger->warning(
|
|
|
|
sprintf(
|
|
|
|
'Unsupported cipher (%s) defined in config.php supported. Falling back to %s',
|
|
|
|
$cipher,
|
|
|
|
self::DEFAULT_CIPHER
|
|
|
|
),
|
2015-03-25 00:29:10 +03:00
|
|
|
['app' => 'encryption']);
|
2015-02-24 21:05:19 +03:00
|
|
|
$cipher = self::DEFAULT_CIPHER;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $cipher;
|
|
|
|
}
|
|
|
|
|
2015-08-07 15:04:17 +03:00
|
|
|
/**
|
|
|
|
* get key size depending on the cipher
|
|
|
|
*
|
2016-01-04 23:00:55 +03:00
|
|
|
* @param string $cipher
|
2015-08-07 15:04:17 +03:00
|
|
|
* @return int
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
protected function getKeySize($cipher) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if (isset($this->supportedCiphersAndKeySize[$cipher])) {
|
2016-01-04 23:00:55 +03:00
|
|
|
return $this->supportedCiphersAndKeySize[$cipher];
|
2015-08-07 15:04:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
throw new \InvalidArgumentException(
|
2016-01-04 23:00:55 +03:00
|
|
|
sprintf(
|
|
|
|
'Unsupported cipher (%s) defined.',
|
|
|
|
$cipher
|
|
|
|
)
|
2015-08-07 15:04:17 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-24 14:02:06 +03:00
|
|
|
/**
|
|
|
|
* get legacy cipher
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLegacyCipher() {
|
2020-08-12 14:22:24 +03:00
|
|
|
if (!$this->supportLegacy) {
|
|
|
|
throw new ServerNotAvailableException('Legacy cipher is no longer supported!');
|
|
|
|
}
|
|
|
|
|
2015-04-24 14:02:06 +03:00
|
|
|
return self::LEGACY_CIPHER;
|
|
|
|
}
|
|
|
|
|
2015-02-24 21:05:19 +03:00
|
|
|
/**
|
2015-04-02 18:53:40 +03:00
|
|
|
* @param string $encryptedContent
|
|
|
|
* @param string $iv
|
2015-02-24 21:05:19 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function concatIV($encryptedContent, $iv) {
|
|
|
|
return $encryptedContent . '00iv00' . $iv;
|
|
|
|
}
|
|
|
|
|
2016-01-05 14:51:05 +03:00
|
|
|
/**
|
|
|
|
* @param string $encryptedContent
|
|
|
|
* @param string $signature
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function concatSig($encryptedContent, $signature) {
|
|
|
|
return $encryptedContent . '00sig00' . $signature;
|
|
|
|
}
|
|
|
|
|
2015-02-24 21:05:19 +03:00
|
|
|
/**
|
2016-01-05 01:10:51 +03:00
|
|
|
* Note: This is _NOT_ a padding used for encryption purposes. It is solely
|
|
|
|
* used to achieve the PHP stream size. It has _NOTHING_ to do with the
|
|
|
|
* encrypted content and is not used in any crypto primitive.
|
|
|
|
*
|
2015-11-27 18:32:44 +03:00
|
|
|
* @param string $data
|
2015-02-24 21:05:19 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function addPadding($data) {
|
2016-01-05 14:51:05 +03:00
|
|
|
return $data . 'xxx';
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
2015-08-07 15:04:17 +03:00
|
|
|
/**
|
|
|
|
* generate password hash used to encrypt the users private key
|
|
|
|
*
|
|
|
|
* @param string $password
|
|
|
|
* @param string $cipher
|
2015-08-07 16:51:43 +03:00
|
|
|
* @param string $uid only used for user keys
|
2015-08-07 15:04:17 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
2015-08-07 16:51:43 +03:00
|
|
|
protected function generatePasswordHash($password, $cipher, $uid = '') {
|
2015-08-07 15:04:17 +03:00
|
|
|
$instanceId = $this->config->getSystemValue('instanceid');
|
|
|
|
$instanceSecret = $this->config->getSystemValue('secret');
|
2015-08-07 16:51:43 +03:00
|
|
|
$salt = hash('sha256', $uid . $instanceId . $instanceSecret, true);
|
2015-08-07 15:04:17 +03:00
|
|
|
$keySize = $this->getKeySize($cipher);
|
|
|
|
|
2015-12-11 08:17:47 +03:00
|
|
|
$hash = hash_pbkdf2(
|
|
|
|
'sha256',
|
|
|
|
$password,
|
|
|
|
$salt,
|
|
|
|
100000,
|
|
|
|
$keySize,
|
|
|
|
true
|
|
|
|
);
|
2015-08-07 15:04:17 +03:00
|
|
|
|
|
|
|
return $hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* encrypt private key
|
|
|
|
*
|
|
|
|
* @param string $privateKey
|
|
|
|
* @param string $password
|
2015-08-07 16:51:43 +03:00
|
|
|
* @param string $uid for regular users, empty for system keys
|
2015-11-27 18:32:44 +03:00
|
|
|
* @return false|string
|
2015-08-07 15:04:17 +03:00
|
|
|
*/
|
2015-08-07 16:51:43 +03:00
|
|
|
public function encryptPrivateKey($privateKey, $password, $uid = '') {
|
2015-08-07 15:04:17 +03:00
|
|
|
$cipher = $this->getCipher();
|
2015-08-07 16:51:43 +03:00
|
|
|
$hash = $this->generatePasswordHash($password, $cipher, $uid);
|
2015-08-07 15:04:17 +03:00
|
|
|
$encryptedKey = $this->symmetricEncryptFileContent(
|
|
|
|
$privateKey,
|
2016-02-02 22:00:36 +03:00
|
|
|
$hash,
|
2016-02-03 16:32:04 +03:00
|
|
|
0,
|
2016-02-02 22:00:36 +03:00
|
|
|
0
|
2015-08-07 15:04:17 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
return $encryptedKey;
|
|
|
|
}
|
|
|
|
|
2015-02-24 21:05:19 +03:00
|
|
|
/**
|
2015-04-08 15:19:11 +03:00
|
|
|
* @param string $privateKey
|
2015-04-02 18:53:40 +03:00
|
|
|
* @param string $password
|
2015-08-07 16:51:43 +03:00
|
|
|
* @param string $uid for regular users, empty for system keys
|
2015-11-27 18:32:44 +03:00
|
|
|
* @return false|string
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
2015-08-07 16:51:43 +03:00
|
|
|
public function decryptPrivateKey($privateKey, $password = '', $uid = '') {
|
2015-04-08 15:19:11 +03:00
|
|
|
$header = $this->parseHeader($privateKey);
|
|
|
|
|
|
|
|
if (isset($header['cipher'])) {
|
|
|
|
$cipher = $header['cipher'];
|
|
|
|
} else {
|
2020-08-12 14:22:24 +03:00
|
|
|
$cipher = $this->getLegacyCipher();
|
2015-04-08 15:19:11 +03:00
|
|
|
}
|
2015-02-24 21:05:19 +03:00
|
|
|
|
2015-08-07 15:04:17 +03:00
|
|
|
if (isset($header['keyFormat'])) {
|
|
|
|
$keyFormat = $header['keyFormat'];
|
|
|
|
} else {
|
|
|
|
$keyFormat = self::LEGACY_KEY_FORMAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($keyFormat === 'hash') {
|
2015-08-07 16:51:43 +03:00
|
|
|
$password = $this->generatePasswordHash($password, $cipher, $uid);
|
2015-08-07 15:04:17 +03:00
|
|
|
}
|
|
|
|
|
2015-02-24 21:05:19 +03:00
|
|
|
// If we found a header we need to remove it from the key we want to decrypt
|
|
|
|
if (!empty($header)) {
|
2015-04-08 15:19:11 +03:00
|
|
|
$privateKey = substr($privateKey,
|
|
|
|
strpos($privateKey,
|
2015-04-14 13:23:25 +03:00
|
|
|
self::HEADER_END) + strlen(self::HEADER_END));
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
2016-02-02 22:00:36 +03:00
|
|
|
$plainKey = $this->symmetricDecryptFileContent(
|
|
|
|
$privateKey,
|
2015-03-25 00:29:10 +03:00
|
|
|
$password,
|
2016-02-02 22:00:36 +03:00
|
|
|
$cipher,
|
|
|
|
0
|
|
|
|
);
|
2015-02-24 21:05:19 +03:00
|
|
|
|
2015-08-07 15:04:17 +03:00
|
|
|
if ($this->isValidPrivateKey($plainKey) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $plainKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if it is a valid private key
|
|
|
|
*
|
2015-11-27 18:32:44 +03:00
|
|
|
* @param string $plainKey
|
2015-08-07 15:04:17 +03:00
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isValidPrivateKey($plainKey) {
|
2015-02-24 21:05:19 +03:00
|
|
|
$res = openssl_get_privatekey($plainKey);
|
2020-11-30 15:23:02 +03:00
|
|
|
// TODO: remove resource check one php7.4 is not longer supported
|
|
|
|
if (is_resource($res) || (is_object($res) && get_class($res) === 'OpenSSLAsymmetricKey')) {
|
2015-02-24 21:05:19 +03:00
|
|
|
$sslInfo = openssl_pkey_get_details($res);
|
2015-08-07 15:04:17 +03:00
|
|
|
if (isset($sslInfo['key'])) {
|
|
|
|
return true;
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-25 16:58:07 +03:00
|
|
|
return false;
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-27 18:32:44 +03:00
|
|
|
* @param string $keyFileContents
|
2015-04-01 14:58:23 +03:00
|
|
|
* @param string $passPhrase
|
2015-02-24 21:05:19 +03:00
|
|
|
* @param string $cipher
|
2016-02-02 22:00:36 +03:00
|
|
|
* @param int $version
|
2020-10-13 11:51:22 +03:00
|
|
|
* @param int|string $position
|
2015-04-01 14:58:23 +03:00
|
|
|
* @return string
|
2015-02-24 21:05:19 +03:00
|
|
|
* @throws DecryptionFailedException
|
|
|
|
*/
|
2016-02-03 16:32:04 +03:00
|
|
|
public function symmetricDecryptFileContent($keyFileContents, $passPhrase, $cipher = self::DEFAULT_CIPHER, $version = 0, $position = 0) {
|
2018-08-21 15:14:59 +03:00
|
|
|
if ($keyFileContents == '') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2016-01-05 14:51:05 +03:00
|
|
|
$catFile = $this->splitMetaData($keyFileContents, $cipher);
|
|
|
|
|
2016-01-05 21:01:03 +03:00
|
|
|
if ($catFile['signature'] !== false) {
|
2020-08-11 20:26:41 +03:00
|
|
|
try {
|
|
|
|
// First try the new format
|
|
|
|
$this->checkSignature($catFile['encrypted'], $passPhrase . '_' . $version . '_' . $position, $catFile['signature']);
|
|
|
|
} catch (GenericEncryptionException $e) {
|
|
|
|
// For compatibility with old files check the version without _
|
|
|
|
$this->checkSignature($catFile['encrypted'], $passPhrase . $version . $position, $catFile['signature']);
|
|
|
|
}
|
2016-01-05 14:51:05 +03:00
|
|
|
}
|
2015-02-24 21:05:19 +03:00
|
|
|
|
2015-04-01 14:58:23 +03:00
|
|
|
return $this->decrypt($catFile['encrypted'],
|
2015-03-25 00:29:10 +03:00
|
|
|
$catFile['iv'],
|
2015-04-01 14:58:23 +03:00
|
|
|
$passPhrase,
|
2015-03-25 00:29:10 +03:00
|
|
|
$cipher);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-05 14:51:05 +03:00
|
|
|
* check for valid signature
|
2015-04-09 15:59:03 +03:00
|
|
|
*
|
2016-01-05 14:51:05 +03:00
|
|
|
* @param string $data
|
|
|
|
* @param string $passPhrase
|
|
|
|
* @param string $expectedSignature
|
2017-03-14 13:31:46 +03:00
|
|
|
* @throws GenericEncryptionException
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
2016-01-05 14:51:05 +03:00
|
|
|
private function checkSignature($data, $passPhrase, $expectedSignature) {
|
2018-11-30 17:24:05 +03:00
|
|
|
$enforceSignature = !$this->config->getSystemValue('encryption_skip_signature_check', false);
|
2018-11-27 13:08:41 +03:00
|
|
|
|
2016-01-05 14:51:05 +03:00
|
|
|
$signature = $this->createSignature($data, $passPhrase);
|
2018-11-30 17:24:05 +03:00
|
|
|
$isCorrectHash = hash_equals($expectedSignature, $signature);
|
2018-11-27 13:08:41 +03:00
|
|
|
|
2018-11-30 17:24:05 +03:00
|
|
|
if (!$isCorrectHash && $enforceSignature) {
|
2017-03-14 13:31:46 +03:00
|
|
|
throw new GenericEncryptionException('Bad Signature', $this->l->t('Bad Signature'));
|
2020-04-10 11:35:09 +03:00
|
|
|
} elseif (!$isCorrectHash && !$enforceSignature) {
|
2018-11-27 13:08:41 +03:00
|
|
|
$this->logger->info("Signature check skipped", ['app' => 'encryption']);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-05 14:51:05 +03:00
|
|
|
* create signature
|
2015-04-09 15:59:03 +03:00
|
|
|
*
|
2016-01-05 14:51:05 +03:00
|
|
|
* @param string $data
|
|
|
|
* @param string $passPhrase
|
2015-11-27 18:32:44 +03:00
|
|
|
* @return string
|
2015-02-24 21:05:19 +03:00
|
|
|
*/
|
2016-01-05 14:51:05 +03:00
|
|
|
private function createSignature($data, $passPhrase) {
|
2016-01-08 20:07:19 +03:00
|
|
|
$passPhrase = hash('sha512', $passPhrase . 'a', true);
|
2018-02-13 23:55:24 +03:00
|
|
|
return hash_hmac('sha256', $data, $passPhrase);
|
2016-01-05 14:51:05 +03:00
|
|
|
}
|
2015-02-24 21:05:19 +03:00
|
|
|
|
|
|
|
|
2016-01-05 14:51:05 +03:00
|
|
|
/**
|
|
|
|
* remove padding
|
|
|
|
*
|
|
|
|
* @param string $padded
|
|
|
|
* @param bool $hasSignature did the block contain a signature, in this case we use a different padding
|
|
|
|
* @return string|false
|
|
|
|
*/
|
|
|
|
private function removePadding($padded, $hasSignature = false) {
|
|
|
|
if ($hasSignature === false && substr($padded, -2) === 'xx') {
|
|
|
|
return substr($padded, 0, -2);
|
|
|
|
} elseif ($hasSignature === true && substr($padded, -3) === 'xxx') {
|
|
|
|
return substr($padded, 0, -3);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-02-24 21:05:19 +03:00
|
|
|
|
2016-01-05 14:51:05 +03:00
|
|
|
/**
|
|
|
|
* split meta data from encrypted file
|
|
|
|
* Note: for now, we assume that the meta data always start with the iv
|
|
|
|
* followed by the signature, if available
|
|
|
|
*
|
|
|
|
* @param string $catFile
|
|
|
|
* @param string $cipher
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function splitMetaData($catFile, $cipher) {
|
|
|
|
if ($this->hasSignature($catFile, $cipher)) {
|
|
|
|
$catFile = $this->removePadding($catFile, true);
|
|
|
|
$meta = substr($catFile, -93);
|
|
|
|
$iv = substr($meta, strlen('00iv00'), 16);
|
|
|
|
$sig = substr($meta, 22 + strlen('00sig00'));
|
|
|
|
$encrypted = substr($catFile, 0, -93);
|
|
|
|
} else {
|
|
|
|
$catFile = $this->removePadding($catFile);
|
|
|
|
$meta = substr($catFile, -22);
|
|
|
|
$iv = substr($meta, -16);
|
|
|
|
$sig = false;
|
2016-01-05 17:29:44 +03:00
|
|
|
$encrypted = substr($catFile, 0, -22);
|
2016-01-05 14:51:05 +03:00
|
|
|
}
|
2015-02-24 21:05:19 +03:00
|
|
|
|
|
|
|
return [
|
|
|
|
'encrypted' => $encrypted,
|
2016-01-05 14:51:05 +03:00
|
|
|
'iv' => $iv,
|
|
|
|
'signature' => $sig
|
2015-02-24 21:05:19 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-01-05 14:51:05 +03:00
|
|
|
/**
|
|
|
|
* check if encrypted block is signed
|
|
|
|
*
|
|
|
|
* @param string $catFile
|
|
|
|
* @param string $cipher
|
|
|
|
* @return bool
|
2017-03-14 13:31:46 +03:00
|
|
|
* @throws GenericEncryptionException
|
2016-01-05 14:51:05 +03:00
|
|
|
*/
|
|
|
|
private function hasSignature($catFile, $cipher) {
|
2018-11-28 01:38:41 +03:00
|
|
|
$skipSignatureCheck = $this->config->getSystemValue('encryption_skip_signature_check', false);
|
|
|
|
|
2016-01-05 18:55:58 +03:00
|
|
|
$meta = substr($catFile, -93);
|
2016-01-05 14:51:05 +03:00
|
|
|
$signaturePosition = strpos($meta, '00sig00');
|
|
|
|
|
2020-08-12 14:22:24 +03:00
|
|
|
// If we no longer support the legacy format then everything needs a signature
|
|
|
|
if (!$skipSignatureCheck && !$this->supportLegacy && $signaturePosition === false) {
|
|
|
|
throw new GenericEncryptionException('Missing Signature', $this->l->t('Missing Signature'));
|
|
|
|
}
|
|
|
|
|
2016-01-05 14:51:05 +03:00
|
|
|
// enforce signature for the new 'CTR' ciphers
|
2018-11-28 01:38:41 +03:00
|
|
|
if (!$skipSignatureCheck && $signaturePosition === false && stripos($cipher, 'ctr') !== false) {
|
2017-03-14 13:31:46 +03:00
|
|
|
throw new GenericEncryptionException('Missing Signature', $this->l->t('Missing Signature'));
|
2016-01-05 14:51:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ($signaturePosition !== false);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-02-24 21:05:19 +03:00
|
|
|
/**
|
2015-11-27 18:32:44 +03:00
|
|
|
* @param string $encryptedContent
|
|
|
|
* @param string $iv
|
2015-04-01 14:58:23 +03:00
|
|
|
* @param string $passPhrase
|
2015-02-24 21:05:19 +03:00
|
|
|
* @param string $cipher
|
|
|
|
* @return string
|
|
|
|
* @throws DecryptionFailedException
|
|
|
|
*/
|
2015-04-01 14:58:23 +03:00
|
|
|
private function decrypt($encryptedContent, $iv, $passPhrase = '', $cipher = self::DEFAULT_CIPHER) {
|
2015-03-25 00:29:10 +03:00
|
|
|
$plainContent = openssl_decrypt($encryptedContent,
|
|
|
|
$cipher,
|
2015-04-01 14:58:23 +03:00
|
|
|
$passPhrase,
|
2015-03-25 00:29:10 +03:00
|
|
|
false,
|
|
|
|
$iv);
|
2015-02-24 21:05:19 +03:00
|
|
|
|
|
|
|
if ($plainContent) {
|
|
|
|
return $plainContent;
|
|
|
|
} else {
|
2015-04-01 14:58:23 +03:00
|
|
|
throw new DecryptionFailedException('Encryption library: Decryption (symmetric) of content failed: ' . openssl_error_string());
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-27 18:32:44 +03:00
|
|
|
* @param string $data
|
2015-02-24 21:05:19 +03:00
|
|
|
* @return array
|
|
|
|
*/
|
2015-08-07 15:04:17 +03:00
|
|
|
protected function parseHeader($data) {
|
2015-02-24 21:05:19 +03:00
|
|
|
$result = [];
|
|
|
|
|
2015-04-02 18:53:40 +03:00
|
|
|
if (substr($data, 0, strlen(self::HEADER_START)) === self::HEADER_START) {
|
|
|
|
$endAt = strpos($data, self::HEADER_END);
|
|
|
|
$header = substr($data, 0, $endAt + strlen(self::HEADER_END));
|
2015-02-24 21:05:19 +03:00
|
|
|
|
|
|
|
// +1 not to start with an ':' which would result in empty element at the beginning
|
2015-03-25 00:29:10 +03:00
|
|
|
$exploded = explode(':',
|
2015-04-02 18:53:40 +03:00
|
|
|
substr($header, strlen(self::HEADER_START) + 1));
|
2015-02-24 21:05:19 +03:00
|
|
|
|
|
|
|
$element = array_shift($exploded);
|
|
|
|
|
2017-05-10 14:58:15 +03:00
|
|
|
while ($element !== self::HEADER_END) {
|
2015-02-24 21:05:19 +03:00
|
|
|
$result[$element] = array_shift($exploded);
|
|
|
|
$element = array_shift($exploded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-09 15:59:03 +03:00
|
|
|
* generate initialization vector
|
|
|
|
*
|
2015-02-24 21:05:19 +03:00
|
|
|
* @return string
|
|
|
|
* @throws GenericEncryptionException
|
|
|
|
*/
|
|
|
|
private function generateIv() {
|
2016-01-05 01:06:23 +03:00
|
|
|
return random_bytes(16);
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|
2015-03-25 00:29:10 +03:00
|
|
|
|
2015-03-26 14:23:36 +03:00
|
|
|
/**
|
2016-01-08 20:07:19 +03:00
|
|
|
* Generate a cryptographically secure pseudo-random 256-bit ASCII key, used
|
|
|
|
* as file key
|
2015-04-09 15:06:55 +03:00
|
|
|
*
|
2015-03-26 14:23:36 +03:00
|
|
|
* @return string
|
2015-04-09 15:06:55 +03:00
|
|
|
* @throws \Exception
|
2015-03-26 14:23:36 +03:00
|
|
|
*/
|
2015-04-28 19:06:46 +03:00
|
|
|
public function generateFileKey() {
|
2016-01-08 20:07:19 +03:00
|
|
|
return random_bytes(32);
|
2015-03-26 14:23:36 +03:00
|
|
|
}
|
|
|
|
|
2015-03-25 00:29:10 +03:00
|
|
|
/**
|
|
|
|
* @param $encKeyFile
|
|
|
|
* @param $shareKey
|
|
|
|
* @param $privateKey
|
2015-11-27 18:32:44 +03:00
|
|
|
* @return string
|
2015-03-25 00:29:10 +03:00
|
|
|
* @throws MultiKeyDecryptException
|
|
|
|
*/
|
|
|
|
public function multiKeyDecrypt($encKeyFile, $shareKey, $privateKey) {
|
|
|
|
if (!$encKeyFile) {
|
|
|
|
throw new MultiKeyDecryptException('Cannot multikey decrypt empty plain content');
|
|
|
|
}
|
|
|
|
|
2020-12-03 18:31:02 +03:00
|
|
|
if (openssl_open($encKeyFile, $plainContent, $shareKey, $privateKey, 'RC4')) {
|
2015-03-25 00:29:10 +03:00
|
|
|
return $plainContent;
|
|
|
|
} else {
|
2015-04-01 14:58:23 +03:00
|
|
|
throw new MultiKeyDecryptException('multikeydecrypt with share key failed:' . openssl_error_string());
|
2015-03-25 00:29:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-09 15:06:55 +03:00
|
|
|
* @param string $plainContent
|
2015-03-25 00:29:10 +03:00
|
|
|
* @param array $keyFiles
|
|
|
|
* @return array
|
|
|
|
* @throws MultiKeyEncryptException
|
|
|
|
*/
|
|
|
|
public function multiKeyEncrypt($plainContent, array $keyFiles) {
|
|
|
|
// openssl_seal returns false without errors if plaincontent is empty
|
|
|
|
// so trigger our own error
|
|
|
|
if (empty($plainContent)) {
|
|
|
|
throw new MultiKeyEncryptException('Cannot multikeyencrypt empty plain content');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set empty vars to be set by openssl by reference
|
|
|
|
$sealed = '';
|
|
|
|
$shareKeys = [];
|
|
|
|
$mappedShareKeys = [];
|
|
|
|
|
2020-12-03 18:31:02 +03:00
|
|
|
if (openssl_seal($plainContent, $sealed, $shareKeys, $keyFiles, 'RC4')) {
|
2015-03-25 00:29:10 +03:00
|
|
|
$i = 0;
|
|
|
|
|
2015-04-01 14:58:23 +03:00
|
|
|
// Ensure each shareKey is labelled with its corresponding key id
|
2015-03-25 00:29:10 +03:00
|
|
|
foreach ($keyFiles as $userId => $publicKey) {
|
|
|
|
$mappedShareKeys[$userId] = $shareKeys[$i];
|
|
|
|
$i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return [
|
|
|
|
'keys' => $mappedShareKeys,
|
|
|
|
'data' => $sealed
|
|
|
|
];
|
|
|
|
} else {
|
|
|
|
throw new MultiKeyEncryptException('multikeyencryption failed ' . openssl_error_string());
|
|
|
|
}
|
|
|
|
}
|
2015-02-24 21:05:19 +03:00
|
|
|
}
|