oauth2-server/src/CryptTrait.php

147 lines
3.8 KiB
PHP
Raw Normal View History

2016-01-14 23:44:39 +00:00
<?php
/**
2016-02-19 18:09:39 -05:00
* Public/private key encryption.
2016-01-14 23:44:39 +00:00
*
* @author Alex Bilbie <hello@alexbilbie.com>
* @copyright Copyright (c) Alex Bilbie
* @license http://mit-license.org/
2016-02-19 18:09:39 -05:00
*
2016-01-14 23:44:39 +00:00
* @link https://github.com/thephpleague/oauth2-server
*/
2016-07-09 01:00:44 +02:00
2016-03-17 14:37:21 +00:00
namespace League\OAuth2\Server;
2016-01-14 23:44:39 +00:00
use Defuse\Crypto\Crypto;
2016-03-17 14:37:21 +00:00
trait CryptTrait
2016-01-14 23:44:39 +00:00
{
2016-03-17 14:37:21 +00:00
/**
2016-07-09 01:00:44 +02:00
* @var CryptKey
2016-03-17 14:37:21 +00:00
*/
2016-03-28 16:42:34 +02:00
protected $privateKey;
2016-03-17 14:37:21 +00:00
/**
2016-07-09 01:00:44 +02:00
* @var CryptKey
2016-03-17 14:37:21 +00:00
*/
2016-03-28 16:42:34 +02:00
protected $publicKey;
2016-03-17 14:37:21 +00:00
/**
* @var string
*/
protected $encryptionKey;
2016-03-17 14:37:21 +00:00
/**
* Set path to private key.
*
2016-07-09 01:00:44 +02:00
* @param CryptKey $privateKey
2016-03-17 14:37:21 +00:00
*/
2016-03-28 16:42:34 +02:00
public function setPrivateKey(CryptKey $privateKey)
2016-03-17 14:37:21 +00:00
{
2016-03-28 16:42:34 +02:00
$this->privateKey = $privateKey;
2016-03-17 14:37:21 +00:00
}
/**
* Set path to public key.
*
2016-07-09 01:00:44 +02:00
* @param CryptKey $publicKey
2016-03-17 14:37:21 +00:00
*/
2016-03-28 16:42:34 +02:00
public function setPublicKey(CryptKey $publicKey)
2016-03-17 14:37:21 +00:00
{
2016-03-28 16:42:34 +02:00
$this->publicKey = $publicKey;
2016-03-17 14:37:21 +00:00
}
2016-01-14 23:44:39 +00:00
/**
2016-02-19 18:09:39 -05:00
* Encrypt data with a private key.
2016-01-14 23:44:39 +00:00
*
* @param string $unencryptedData
*
2016-07-09 01:00:44 +02:00
* @throws \LogicException
*
2016-01-14 23:44:39 +00:00
* @return string
*/
2016-03-17 14:37:21 +00:00
protected function encrypt($unencryptedData)
2016-01-14 23:44:39 +00:00
{
if ($this->encryptionKey !== null) {
return Crypto::encryptWithPassword($unencryptedData, $this->encryptionKey);
}
2016-03-28 16:42:34 +02:00
$privateKey = openssl_pkey_get_private($this->privateKey->getKeyPath(), $this->privateKey->getPassPhrase());
2016-01-14 23:44:39 +00:00
$privateKeyDetails = @openssl_pkey_get_details($privateKey);
if ($privateKeyDetails === null) {
2016-03-28 16:42:34 +02:00
throw new \LogicException(
sprintf('Could not get details of private key: %s', $this->privateKey->getKeyPath())
);
2016-01-14 23:44:39 +00:00
}
$chunkSize = ceil($privateKeyDetails['bits'] / 8) - 11;
$output = '';
while ($unencryptedData) {
$chunk = substr($unencryptedData, 0, $chunkSize);
$unencryptedData = substr($unencryptedData, $chunkSize);
if (openssl_private_encrypt($chunk, $encrypted, $privateKey) === false) {
2016-02-12 18:08:27 +00:00
// @codeCoverageIgnoreStart
2016-01-14 23:44:39 +00:00
throw new \LogicException('Failed to encrypt data');
2016-02-12 18:08:27 +00:00
// @codeCoverageIgnoreEnd
2016-01-14 23:44:39 +00:00
}
$output .= $encrypted;
}
2016-03-28 16:42:34 +02:00
openssl_pkey_free($privateKey);
2016-01-14 23:44:39 +00:00
return base64_encode($output);
}
/**
2016-02-19 18:09:39 -05:00
* Decrypt data with a public key.
2016-01-14 23:44:39 +00:00
*
* @param string $encryptedData
*
2016-02-12 10:00:41 +00:00
* @throws \LogicException
*
2016-01-14 23:44:39 +00:00
* @return string
*/
2016-03-17 14:37:21 +00:00
protected function decrypt($encryptedData)
2016-01-14 23:44:39 +00:00
{
if ($this->encryptionKey !== null) {
return Crypto::decryptWithPassword($encryptedData, $this->encryptionKey);
}
2016-03-28 16:42:34 +02:00
$publicKey = openssl_pkey_get_public($this->publicKey->getKeyPath());
2016-01-14 23:44:39 +00:00
$publicKeyDetails = @openssl_pkey_get_details($publicKey);
if ($publicKeyDetails === null) {
2016-03-28 16:42:34 +02:00
throw new \LogicException(
sprintf('Could not get details of public key: %s', $this->publicKey->getKeyPath())
);
2016-01-14 23:44:39 +00:00
}
$chunkSize = ceil($publicKeyDetails['bits'] / 8);
$output = '';
$encryptedData = base64_decode($encryptedData);
while ($encryptedData) {
$chunk = substr($encryptedData, 0, $chunkSize);
$encryptedData = substr($encryptedData, $chunkSize);
if (openssl_public_decrypt($chunk, $decrypted, $publicKey/*, OPENSSL_PKCS1_OAEP_PADDING*/) === false) {
2016-02-12 18:08:27 +00:00
// @codeCoverageIgnoreStart
2016-01-14 23:44:39 +00:00
throw new \LogicException('Failed to decrypt data');
2016-02-12 18:08:27 +00:00
// @codeCoverageIgnoreEnd
2016-01-14 23:44:39 +00:00
}
$output .= $decrypted;
}
2016-03-28 16:42:34 +02:00
openssl_pkey_free($publicKey);
2016-01-14 23:44:39 +00:00
return $output;
}
/**
* Set the encryption key
*
* @param string $key
*/
public function setEncryptionKey($key = null)
{
$this->encryptionKey = $key;
}
2016-03-17 10:37:48 -04:00
}