2015-02-24 18:05:19 +00:00
|
|
|
<?php
|
|
|
|
/**
|
2015-04-07 15:02:49 +00:00
|
|
|
* @author Björn Schießle <schiessle@owncloud.com>
|
|
|
|
* @author Clark Tomlinson <fallen013@gmail.com>
|
2015-06-25 09:43:55 +00:00
|
|
|
* @author Lukas Reschke <lukas@owncloud.com>
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2015-04-07 15:02:49 +00:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
|
|
|
*
|
2015-02-24 18:05:19 +00:00
|
|
|
* @copyright Copyright (c) 2015, ownCloud, Inc.
|
|
|
|
* @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\Crypto;
|
|
|
|
|
|
|
|
|
|
|
|
use OC\Encryption\Exceptions\DecryptionFailedException;
|
|
|
|
use OC\Encryption\Exceptions\EncryptionFailedException;
|
2015-04-01 11:58:23 +00:00
|
|
|
use OCA\Encryption\Exceptions\MultiKeyDecryptException;
|
|
|
|
use OCA\Encryption\Exceptions\MultiKeyEncryptException;
|
2015-08-07 12:04:17 +00:00
|
|
|
use OCA\Encryption\Vendor\PBKDF2Fallback;
|
2015-04-01 14:36:08 +00:00
|
|
|
use OCP\Encryption\Exceptions\GenericEncryptionException;
|
2015-02-24 18:05:19 +00:00
|
|
|
use OCP\IConfig;
|
|
|
|
use OCP\ILogger;
|
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserSession;
|
|
|
|
|
|
|
|
class Crypt {
|
|
|
|
|
|
|
|
const DEFAULT_CIPHER = 'AES-256-CFB';
|
2015-04-08 12:19:11 +00:00
|
|
|
// default cipher from old ownCloud versions
|
|
|
|
const LEGACY_CIPHER = 'AES-128-CFB';
|
2015-02-24 18:05:19 +00:00
|
|
|
|
2015-08-07 12:04:17 +00:00
|
|
|
// default key format, old ownCloud version encrypted the private key directly
|
|
|
|
// with the user password
|
|
|
|
const LEGACY_KEY_FORMAT = 'password';
|
|
|
|
|
2015-04-02 15:53:40 +00:00
|
|
|
const HEADER_START = 'HBEGIN';
|
|
|
|
const HEADER_END = 'HEND';
|
2015-02-24 18:05:19 +00:00
|
|
|
/**
|
|
|
|
* @var ILogger
|
|
|
|
*/
|
|
|
|
private $logger;
|
|
|
|
/**
|
|
|
|
* @var IUser
|
|
|
|
*/
|
|
|
|
private $user;
|
|
|
|
/**
|
|
|
|
* @var IConfig
|
|
|
|
*/
|
|
|
|
private $config;
|
|
|
|
|
2015-08-07 12:04:17 +00:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
private $supportedKeyFormats;
|
|
|
|
|
2015-02-24 18:05:19 +00:00
|
|
|
/**
|
|
|
|
* @param ILogger $logger
|
|
|
|
* @param IUserSession $userSession
|
|
|
|
* @param IConfig $config
|
|
|
|
*/
|
|
|
|
public function __construct(ILogger $logger, IUserSession $userSession, IConfig $config) {
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->user = $userSession && $userSession->isLoggedIn() ? $userSession->getUser() : false;
|
|
|
|
$this->config = $config;
|
2015-08-07 12:04:17 +00:00
|
|
|
$this->supportedKeyFormats = ['hash', 'password'];
|
2015-02-24 18:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-09 12:59:03 +00:00
|
|
|
* create new private/public key-pair for user
|
|
|
|
*
|
2015-04-02 15:53:40 +00:00
|
|
|
* @return array|bool
|
2015-02-24 18:05:19 +00:00
|
|
|
*/
|
|
|
|
public function createKeyPair() {
|
|
|
|
|
|
|
|
$log = $this->logger;
|
|
|
|
$res = $this->getOpenSSLPKey();
|
|
|
|
|
|
|
|
if (!$res) {
|
2015-04-09 12:06:55 +00:00
|
|
|
$log->error("Encryption Library couldn't generate users key-pair for {$this->user->getUID()}",
|
2015-03-24 21:29:10 +00:00
|
|
|
['app' => 'encryption']);
|
2015-02-24 18:05:19 +00:00
|
|
|
|
|
|
|
if (openssl_error_string()) {
|
2015-03-24 21:29:10 +00:00
|
|
|
$log->error('Encryption library openssl_pkey_new() fails: ' . openssl_error_string(),
|
|
|
|
['app' => 'encryption']);
|
2015-02-24 18:05:19 +00:00
|
|
|
}
|
2015-03-24 21:29:10 +00:00
|
|
|
} elseif (openssl_pkey_export($res,
|
|
|
|
$privateKey,
|
|
|
|
null,
|
|
|
|
$this->getOpenSSLConfig())) {
|
2015-02-24 18:05:19 +00:00
|
|
|
$keyDetails = openssl_pkey_get_details($res);
|
|
|
|
$publicKey = $keyDetails['key'];
|
|
|
|
|
|
|
|
return [
|
|
|
|
'publicKey' => $publicKey,
|
|
|
|
'privateKey' => $privateKey
|
|
|
|
];
|
|
|
|
}
|
2015-04-09 12:06:55 +00:00
|
|
|
$log->error('Encryption library couldn\'t export users private key, please check your servers OpenSSL configuration.' . $this->user->getUID(),
|
2015-03-24 21:29:10 +00:00
|
|
|
['app' => 'encryption']);
|
2015-02-24 18:05:19 +00:00
|
|
|
if (openssl_error_string()) {
|
2015-03-24 21:29:10 +00:00
|
|
|
$log->error('Encryption Library:' . openssl_error_string(),
|
|
|
|
['app' => 'encryption']);
|
2015-02-24 18:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-09 12:59:03 +00:00
|
|
|
* Generates a new private key
|
|
|
|
*
|
2015-02-24 18:05:19 +00:00
|
|
|
* @return resource
|
|
|
|
*/
|
|
|
|
public function getOpenSSLPKey() {
|
|
|
|
$config = $this->getOpenSSLConfig();
|
|
|
|
return openssl_pkey_new($config);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-09 12:59:03 +00:00
|
|
|
* get openSSL Config
|
|
|
|
*
|
2015-02-24 18:05:19 +00:00
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function getOpenSSLConfig() {
|
|
|
|
$config = ['private_key_bits' => 4096];
|
2015-04-09 12:59:03 +00:00
|
|
|
$config = array_merge(
|
|
|
|
$config,
|
|
|
|
$this->config->getSystemValue('openssl', [])
|
|
|
|
);
|
2015-02-24 18:05:19 +00:00
|
|
|
return $config;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-02 15:53:40 +00:00
|
|
|
* @param string $plainContent
|
|
|
|
* @param string $passPhrase
|
2015-02-24 18:05:19 +00:00
|
|
|
* @return bool|string
|
|
|
|
* @throws GenericEncryptionException
|
|
|
|
*/
|
2015-04-01 11:58:23 +00:00
|
|
|
public function symmetricEncryptFileContent($plainContent, $passPhrase) {
|
2015-02-24 18:05:19 +00:00
|
|
|
|
|
|
|
if (!$plainContent) {
|
2015-03-24 21:29:10 +00:00
|
|
|
$this->logger->error('Encryption Library, symmetrical encryption failed no content given',
|
|
|
|
['app' => 'encryption']);
|
2015-02-24 18:05:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$iv = $this->generateIv();
|
|
|
|
|
2015-04-01 11:58:23 +00:00
|
|
|
$encryptedContent = $this->encrypt($plainContent,
|
|
|
|
$iv,
|
|
|
|
$passPhrase,
|
|
|
|
$this->getCipher());
|
|
|
|
// combine content to encrypt the IV identifier and actual IV
|
|
|
|
$catFile = $this->concatIV($encryptedContent, $iv);
|
|
|
|
$padded = $this->addPadding($catFile);
|
2015-02-24 18:05:19 +00:00
|
|
|
|
2015-04-01 11:58:23 +00:00
|
|
|
return $padded;
|
2015-02-24 18:05:19 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:19:11 +00:00
|
|
|
/**
|
|
|
|
* generate header for encrypted file
|
2015-08-07 12:04:17 +00:00
|
|
|
*
|
|
|
|
* @param string $keyFormat (can be 'hash' or 'password')
|
|
|
|
* @return string
|
|
|
|
* @throws \InvalidArgumentException
|
2015-04-08 12:19:11 +00:00
|
|
|
*/
|
2015-08-07 12:04:17 +00: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 12:19:11 +00:00
|
|
|
$cipher = $this->getCipher();
|
2015-08-07 12:04:17 +00:00
|
|
|
|
|
|
|
$header = self::HEADER_START
|
|
|
|
. ':cipher:' . $cipher
|
|
|
|
. ':keyFormat:' . $keyFormat
|
|
|
|
. ':' . self::HEADER_END;
|
2015-04-08 12:19:11 +00:00
|
|
|
|
|
|
|
return $header;
|
|
|
|
}
|
|
|
|
|
2015-02-24 18:05:19 +00:00
|
|
|
/**
|
2015-04-02 15:53:40 +00:00
|
|
|
* @param string $plainContent
|
|
|
|
* @param string $iv
|
2015-04-01 11:58:23 +00:00
|
|
|
* @param string $passPhrase
|
2015-02-24 18:05:19 +00:00
|
|
|
* @param string $cipher
|
|
|
|
* @return string
|
|
|
|
* @throws EncryptionFailedException
|
|
|
|
*/
|
2015-04-01 11:58:23 +00:00
|
|
|
private function encrypt($plainContent, $iv, $passPhrase = '', $cipher = self::DEFAULT_CIPHER) {
|
2015-03-24 21:29:10 +00:00
|
|
|
$encryptedContent = openssl_encrypt($plainContent,
|
|
|
|
$cipher,
|
2015-04-01 11:58:23 +00:00
|
|
|
$passPhrase,
|
2015-03-24 21:29:10 +00:00
|
|
|
false,
|
|
|
|
$iv);
|
2015-02-24 18:05:19 +00:00
|
|
|
|
|
|
|
if (!$encryptedContent) {
|
|
|
|
$error = 'Encryption (symmetric) of content failed';
|
2015-03-24 21:29:10 +00:00
|
|
|
$this->logger->error($error . openssl_error_string(),
|
|
|
|
['app' => 'encryption']);
|
2015-02-24 18:05:19 +00:00
|
|
|
throw new EncryptionFailedException($error);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $encryptedContent;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-09 12:59:03 +00:00
|
|
|
* return Cipher either from config.php or the default cipher defined in
|
|
|
|
* this class
|
|
|
|
*
|
|
|
|
* @return string
|
2015-02-24 18:05:19 +00:00
|
|
|
*/
|
|
|
|
public function getCipher() {
|
|
|
|
$cipher = $this->config->getSystemValue('cipher', self::DEFAULT_CIPHER);
|
2015-03-24 21:29:10 +00:00
|
|
|
if ($cipher !== 'AES-256-CFB' && $cipher !== 'AES-128-CFB') {
|
|
|
|
$this->logger->warning('Wrong cipher defined in config.php only AES-128-CFB and AES-256-CFB are supported. Fall back' . self::DEFAULT_CIPHER,
|
|
|
|
['app' => 'encryption']);
|
2015-02-24 18:05:19 +00:00
|
|
|
$cipher = self::DEFAULT_CIPHER;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $cipher;
|
|
|
|
}
|
|
|
|
|
2015-08-07 12:04:17 +00:00
|
|
|
/**
|
|
|
|
* get key size depending on the cipher
|
|
|
|
*
|
|
|
|
* @param string $cipher supported ('AES-256-CFB' and 'AES-128-CFB')
|
|
|
|
* @return int
|
|
|
|
* @throws \InvalidArgumentException
|
|
|
|
*/
|
|
|
|
protected function getKeySize($cipher) {
|
|
|
|
if ($cipher === 'AES-256-CFB') {
|
|
|
|
return 32;
|
|
|
|
} else if ($cipher === 'AES-128-CFB') {
|
|
|
|
return 16;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new \InvalidArgumentException(
|
|
|
|
'Wrong cipher defined only AES-128-CFB and AES-256-CFB are supported.'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-04-24 11:02:06 +00:00
|
|
|
/**
|
|
|
|
* get legacy cipher
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getLegacyCipher() {
|
|
|
|
return self::LEGACY_CIPHER;
|
|
|
|
}
|
|
|
|
|
2015-02-24 18:05:19 +00:00
|
|
|
/**
|
2015-04-02 15:53:40 +00:00
|
|
|
* @param string $encryptedContent
|
|
|
|
* @param string $iv
|
2015-02-24 18:05:19 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function concatIV($encryptedContent, $iv) {
|
|
|
|
return $encryptedContent . '00iv00' . $iv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
private function addPadding($data) {
|
|
|
|
return $data . 'xx';
|
|
|
|
}
|
|
|
|
|
2015-08-07 12:04:17 +00:00
|
|
|
/**
|
|
|
|
* generate password hash used to encrypt the users private key
|
|
|
|
*
|
|
|
|
* @param string $password
|
|
|
|
* @param string $cipher
|
2015-08-07 13:51:43 +00:00
|
|
|
* @param string $uid only used for user keys
|
2015-08-07 12:04:17 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2015-08-07 13:51:43 +00:00
|
|
|
protected function generatePasswordHash($password, $cipher, $uid = '') {
|
2015-08-07 12:04:17 +00:00
|
|
|
$instanceId = $this->config->getSystemValue('instanceid');
|
|
|
|
$instanceSecret = $this->config->getSystemValue('secret');
|
2015-08-07 13:51:43 +00:00
|
|
|
$salt = hash('sha256', $uid . $instanceId . $instanceSecret, true);
|
2015-08-07 12:04:17 +00:00
|
|
|
$keySize = $this->getKeySize($cipher);
|
|
|
|
|
|
|
|
if (function_exists('hash_pbkdf2')) {
|
|
|
|
$hash = hash_pbkdf2(
|
|
|
|
'sha256',
|
|
|
|
$password,
|
|
|
|
$salt,
|
|
|
|
100000,
|
|
|
|
$keySize,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
// fallback to 3rdparty lib for PHP <= 5.4.
|
|
|
|
// FIXME: Can be removed as soon as support for PHP 5.4 was dropped
|
|
|
|
$fallback = new PBKDF2Fallback();
|
|
|
|
$hash = $fallback->pbkdf2(
|
|
|
|
'sha256',
|
|
|
|
$password,
|
|
|
|
$salt,
|
|
|
|
100000,
|
|
|
|
$keySize,
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* encrypt private key
|
|
|
|
*
|
|
|
|
* @param string $privateKey
|
|
|
|
* @param string $password
|
2015-08-07 13:51:43 +00:00
|
|
|
* @param string $uid for regular users, empty for system keys
|
2015-08-07 12:04:17 +00:00
|
|
|
* @return bool|string
|
|
|
|
*/
|
2015-08-07 13:51:43 +00:00
|
|
|
public function encryptPrivateKey($privateKey, $password, $uid = '') {
|
2015-08-07 12:04:17 +00:00
|
|
|
$cipher = $this->getCipher();
|
2015-08-07 13:51:43 +00:00
|
|
|
$hash = $this->generatePasswordHash($password, $cipher, $uid);
|
2015-08-07 12:04:17 +00:00
|
|
|
$encryptedKey = $this->symmetricEncryptFileContent(
|
|
|
|
$privateKey,
|
|
|
|
$hash
|
|
|
|
);
|
|
|
|
|
|
|
|
return $encryptedKey;
|
|
|
|
}
|
|
|
|
|
2015-02-24 18:05:19 +00:00
|
|
|
/**
|
2015-04-08 12:19:11 +00:00
|
|
|
* @param string $privateKey
|
2015-04-02 15:53:40 +00:00
|
|
|
* @param string $password
|
2015-08-07 13:51:43 +00:00
|
|
|
* @param string $uid for regular users, empty for system keys
|
2015-02-24 18:05:19 +00:00
|
|
|
* @return bool|string
|
|
|
|
*/
|
2015-08-07 13:51:43 +00:00
|
|
|
public function decryptPrivateKey($privateKey, $password = '', $uid = '') {
|
2015-02-24 18:05:19 +00:00
|
|
|
|
2015-04-08 12:19:11 +00:00
|
|
|
$header = $this->parseHeader($privateKey);
|
|
|
|
|
|
|
|
if (isset($header['cipher'])) {
|
|
|
|
$cipher = $header['cipher'];
|
|
|
|
} else {
|
|
|
|
$cipher = self::LEGACY_CIPHER;
|
|
|
|
}
|
2015-02-24 18:05:19 +00:00
|
|
|
|
2015-08-07 12:04:17 +00:00
|
|
|
if (isset($header['keyFormat'])) {
|
|
|
|
$keyFormat = $header['keyFormat'];
|
|
|
|
} else {
|
|
|
|
$keyFormat = self::LEGACY_KEY_FORMAT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($keyFormat === 'hash') {
|
2015-08-07 13:51:43 +00:00
|
|
|
$password = $this->generatePasswordHash($password, $cipher, $uid);
|
2015-08-07 12:04:17 +00:00
|
|
|
}
|
|
|
|
|
2015-02-24 18:05:19 +00:00
|
|
|
// If we found a header we need to remove it from the key we want to decrypt
|
|
|
|
if (!empty($header)) {
|
2015-04-08 12:19:11 +00:00
|
|
|
$privateKey = substr($privateKey,
|
|
|
|
strpos($privateKey,
|
2015-04-14 10:23:25 +00:00
|
|
|
self::HEADER_END) + strlen(self::HEADER_END));
|
2015-02-24 18:05:19 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 12:19:11 +00:00
|
|
|
$plainKey = $this->symmetricDecryptFileContent($privateKey,
|
2015-03-24 21:29:10 +00:00
|
|
|
$password,
|
|
|
|
$cipher);
|
2015-02-24 18:05:19 +00:00
|
|
|
|
2015-08-07 12:04:17 +00:00
|
|
|
if ($this->isValidPrivateKey($plainKey) === false) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $plainKey;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* check if it is a valid private key
|
|
|
|
*
|
|
|
|
* @param $plainKey
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
protected function isValidPrivateKey($plainKey) {
|
2015-02-24 18:05:19 +00:00
|
|
|
$res = openssl_get_privatekey($plainKey);
|
|
|
|
if (is_resource($res)) {
|
|
|
|
$sslInfo = openssl_pkey_get_details($res);
|
2015-08-07 12:04:17 +00:00
|
|
|
if (isset($sslInfo['key'])) {
|
|
|
|
return true;
|
2015-02-24 18:05:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-25 13:58:07 +00:00
|
|
|
return false;
|
2015-02-24 18:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $keyFileContents
|
2015-04-01 11:58:23 +00:00
|
|
|
* @param string $passPhrase
|
2015-02-24 18:05:19 +00:00
|
|
|
* @param string $cipher
|
2015-04-01 11:58:23 +00:00
|
|
|
* @return string
|
2015-02-24 18:05:19 +00:00
|
|
|
* @throws DecryptionFailedException
|
|
|
|
*/
|
2015-04-15 17:37:03 +00:00
|
|
|
public function symmetricDecryptFileContent($keyFileContents, $passPhrase, $cipher = self::DEFAULT_CIPHER) {
|
2015-02-24 18:05:19 +00:00
|
|
|
// Remove Padding
|
|
|
|
$noPadding = $this->removePadding($keyFileContents);
|
|
|
|
|
|
|
|
$catFile = $this->splitIv($noPadding);
|
|
|
|
|
2015-04-01 11:58:23 +00:00
|
|
|
return $this->decrypt($catFile['encrypted'],
|
2015-03-24 21:29:10 +00:00
|
|
|
$catFile['iv'],
|
2015-04-01 11:58:23 +00:00
|
|
|
$passPhrase,
|
2015-03-24 21:29:10 +00:00
|
|
|
$cipher);
|
2015-02-24 18:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-09 12:59:03 +00:00
|
|
|
* remove padding
|
|
|
|
*
|
2015-02-24 18:05:19 +00:00
|
|
|
* @param $padded
|
|
|
|
* @return bool|string
|
|
|
|
*/
|
|
|
|
private function removePadding($padded) {
|
|
|
|
if (substr($padded, -2) === 'xx') {
|
|
|
|
return substr($padded, 0, -2);
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-09 12:59:03 +00:00
|
|
|
* split iv from encrypted content
|
|
|
|
*
|
2015-02-24 18:05:19 +00:00
|
|
|
* @param $catFile
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private function splitIv($catFile) {
|
|
|
|
// Fetch encryption metadata from end of file
|
|
|
|
$meta = substr($catFile, -22);
|
|
|
|
|
|
|
|
// Fetch IV from end of file
|
|
|
|
$iv = substr($meta, -16);
|
|
|
|
|
|
|
|
// Remove IV and IV Identifier text to expose encrypted content
|
|
|
|
|
|
|
|
$encrypted = substr($catFile, 0, -22);
|
|
|
|
|
|
|
|
return [
|
|
|
|
'encrypted' => $encrypted,
|
|
|
|
'iv' => $iv
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $encryptedContent
|
|
|
|
* @param $iv
|
2015-04-01 11:58:23 +00:00
|
|
|
* @param string $passPhrase
|
2015-02-24 18:05:19 +00:00
|
|
|
* @param string $cipher
|
|
|
|
* @return string
|
|
|
|
* @throws DecryptionFailedException
|
|
|
|
*/
|
2015-04-01 11:58:23 +00:00
|
|
|
private function decrypt($encryptedContent, $iv, $passPhrase = '', $cipher = self::DEFAULT_CIPHER) {
|
2015-03-24 21:29:10 +00:00
|
|
|
$plainContent = openssl_decrypt($encryptedContent,
|
|
|
|
$cipher,
|
2015-04-01 11:58:23 +00:00
|
|
|
$passPhrase,
|
2015-03-24 21:29:10 +00:00
|
|
|
false,
|
|
|
|
$iv);
|
2015-02-24 18:05:19 +00:00
|
|
|
|
|
|
|
if ($plainContent) {
|
|
|
|
return $plainContent;
|
|
|
|
} else {
|
2015-04-01 11:58:23 +00:00
|
|
|
throw new DecryptionFailedException('Encryption library: Decryption (symmetric) of content failed: ' . openssl_error_string());
|
2015-02-24 18:05:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-08-07 12:04:17 +00:00
|
|
|
protected function parseHeader($data) {
|
2015-02-24 18:05:19 +00:00
|
|
|
$result = [];
|
|
|
|
|
2015-04-02 15:53:40 +00: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 18:05:19 +00:00
|
|
|
|
|
|
|
// +1 not to start with an ':' which would result in empty element at the beginning
|
2015-03-24 21:29:10 +00:00
|
|
|
$exploded = explode(':',
|
2015-04-02 15:53:40 +00:00
|
|
|
substr($header, strlen(self::HEADER_START) + 1));
|
2015-02-24 18:05:19 +00:00
|
|
|
|
|
|
|
$element = array_shift($exploded);
|
|
|
|
|
2015-04-02 15:53:40 +00:00
|
|
|
while ($element != self::HEADER_END) {
|
2015-02-24 18:05:19 +00:00
|
|
|
$result[$element] = array_shift($exploded);
|
|
|
|
$element = array_shift($exploded);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-09 12:59:03 +00:00
|
|
|
* generate initialization vector
|
|
|
|
*
|
2015-02-24 18:05:19 +00:00
|
|
|
* @return string
|
|
|
|
* @throws GenericEncryptionException
|
|
|
|
*/
|
|
|
|
private function generateIv() {
|
|
|
|
$random = openssl_random_pseudo_bytes(12, $strong);
|
|
|
|
if ($random) {
|
|
|
|
if (!$strong) {
|
|
|
|
// If OpenSSL indicates randomness is insecure log error
|
2015-03-24 21:29:10 +00:00
|
|
|
$this->logger->error('Encryption Library: Insecure symmetric key was generated using openssl_random_psudo_bytes()',
|
|
|
|
['app' => 'encryption']);
|
2015-02-24 18:05:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We encode the iv purely for string manipulation
|
|
|
|
* purposes -it gets decoded before use
|
|
|
|
*/
|
|
|
|
return base64_encode($random);
|
|
|
|
}
|
|
|
|
// If we ever get here we've failed anyway no need for an else
|
|
|
|
throw new GenericEncryptionException('Generating IV Failed');
|
|
|
|
}
|
2015-03-24 21:29:10 +00:00
|
|
|
|
2015-03-26 11:23:36 +00:00
|
|
|
/**
|
2015-04-09 12:06:55 +00:00
|
|
|
* Generate a cryptographically secure pseudo-random base64 encoded 256-bit
|
|
|
|
* ASCII key, used as file key
|
|
|
|
*
|
2015-03-26 11:23:36 +00:00
|
|
|
* @return string
|
2015-04-09 12:06:55 +00:00
|
|
|
* @throws \Exception
|
2015-03-26 11:23:36 +00:00
|
|
|
*/
|
2015-04-28 16:06:46 +00:00
|
|
|
public function generateFileKey() {
|
2015-03-26 11:23:36 +00:00
|
|
|
// Generate key
|
|
|
|
$key = base64_encode(openssl_random_pseudo_bytes(32, $strong));
|
|
|
|
if (!$key || !$strong) {
|
|
|
|
// If OpenSSL indicates randomness is insecure, log error
|
|
|
|
throw new \Exception('Encryption library, Insecure symmetric key was generated using openssl_random_pseudo_bytes()');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $key;
|
|
|
|
}
|
|
|
|
|
2015-03-24 21:29:10 +00:00
|
|
|
/**
|
|
|
|
* @param $encKeyFile
|
|
|
|
* @param $shareKey
|
|
|
|
* @param $privateKey
|
|
|
|
* @return mixed
|
|
|
|
* @throws MultiKeyDecryptException
|
|
|
|
*/
|
|
|
|
public function multiKeyDecrypt($encKeyFile, $shareKey, $privateKey) {
|
|
|
|
if (!$encKeyFile) {
|
|
|
|
throw new MultiKeyDecryptException('Cannot multikey decrypt empty plain content');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (openssl_open($encKeyFile, $plainContent, $shareKey, $privateKey)) {
|
|
|
|
return $plainContent;
|
|
|
|
} else {
|
2015-04-01 11:58:23 +00:00
|
|
|
throw new MultiKeyDecryptException('multikeydecrypt with share key failed:' . openssl_error_string());
|
2015-03-24 21:29:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-04-09 12:06:55 +00:00
|
|
|
* @param string $plainContent
|
2015-03-24 21:29:10 +00: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 = [];
|
|
|
|
|
|
|
|
if (openssl_seal($plainContent, $sealed, $shareKeys, $keyFiles)) {
|
|
|
|
$i = 0;
|
|
|
|
|
2015-04-01 11:58:23 +00:00
|
|
|
// Ensure each shareKey is labelled with its corresponding key id
|
2015-03-24 21:29:10 +00: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 18:05:19 +00:00
|
|
|
}
|
|
|
|
|