| 1 |
<?php |
| 2 |
|
| 3 |
namespace Dudlewebs\WPMCS\s3\Aws\Crypto; |
| 4 |
|
| 5 |
use Dudlewebs\WPMCS\s3\GuzzleHttp\Psr7\StreamDecoratorTrait; |
| 6 |
use LogicException; |
| 7 |
use Dudlewebs\WPMCS\s3\Psr\Http\Message\StreamInterface; |
| 8 |
use Dudlewebs\WPMCS\s3\Aws\Crypto\Cipher\CipherMethod; |
| 9 |
/** |
| 10 |
* @internal Represents a stream of data to be decrypted with passed cipher. |
| 11 |
*/ |
| 12 |
class AesDecryptingStream implements AesStreamInterface |
| 13 |
{ |
| 14 |
const BLOCK_SIZE = 16; |
| 15 |
// 128 bits |
| 16 |
use StreamDecoratorTrait; |
| 17 |
/** |
| 18 |
* @var string |
| 19 |
*/ |
| 20 |
private $buffer = ''; |
| 21 |
/** |
| 22 |
* @var CipherMethod |
| 23 |
*/ |
| 24 |
private $cipherMethod; |
| 25 |
/** |
| 26 |
* @var string |
| 27 |
*/ |
| 28 |
private $key; |
| 29 |
/** |
| 30 |
* @var StreamInterface |
| 31 |
*/ |
| 32 |
private $stream; |
| 33 |
/** |
| 34 |
* @param StreamInterface $cipherText |
| 35 |
* @param string $key |
| 36 |
* @param CipherMethod $cipherMethod |
| 37 |
*/ |
| 38 |
public function __construct(StreamInterface $cipherText, $key, CipherMethod $cipherMethod) |
| 39 |
{ |
| 40 |
$this->stream = $cipherText; |
| 41 |
$this->key = $key; |
| 42 |
$this->cipherMethod = clone $cipherMethod; |
| 43 |
} |
| 44 |
public function getOpenSslName() |
| 45 |
{ |
| 46 |
return $this->cipherMethod->getOpenSslName(); |
| 47 |
} |
| 48 |
public function getAesName() |
| 49 |
{ |
| 50 |
return $this->cipherMethod->getAesName(); |
| 51 |
} |
| 52 |
public function getCurrentIv() |
| 53 |
{ |
| 54 |
return $this->cipherMethod->getCurrentIv(); |
| 55 |
} |
| 56 |
public function getSize() |
| 57 |
{ |
| 58 |
$plainTextSize = $this->stream->getSize(); |
| 59 |
if ($this->cipherMethod->requiresPadding()) { |
| 60 |
// PKCS7 padding requires that between 1 and self::BLOCK_SIZE be |
| 61 |
// added to the plaintext to make it an even number of blocks. The |
| 62 |
// plaintext is between strlen($cipherText) - self::BLOCK_SIZE and |
| 63 |
// strlen($cipherText) - 1 |
| 64 |
return null; |
| 65 |
} |
| 66 |
return $plainTextSize; |
| 67 |
} |
| 68 |
public function isWritable() |
| 69 |
{ |
| 70 |
return \false; |
| 71 |
} |
| 72 |
public function read($length) |
| 73 |
{ |
| 74 |
if ($length > \strlen($this->buffer)) { |
| 75 |
$this->buffer .= $this->decryptBlock((int) (self::BLOCK_SIZE * \ceil(($length - \strlen($this->buffer)) / self::BLOCK_SIZE))); |
| 76 |
} |
| 77 |
$data = \substr($this->buffer, 0, $length); |
| 78 |
$this->buffer = \substr($this->buffer, $length); |
| 79 |
return $data ? $data : ''; |
| 80 |
} |
| 81 |
public function seek($offset, $whence = \SEEK_SET) |
| 82 |
{ |
| 83 |
if ($offset === 0 && $whence === \SEEK_SET) { |
| 84 |
$this->buffer = ''; |
| 85 |
$this->cipherMethod->seek(0, \SEEK_SET); |
| 86 |
$this->stream->seek(0, \SEEK_SET); |
| 87 |
} else { |
| 88 |
throw new LogicException('AES encryption streams only support being' . ' rewound, not arbitrary seeking.'); |
| 89 |
} |
| 90 |
} |
| 91 |
private function decryptBlock($length) |
| 92 |
{ |
| 93 |
if ($this->stream->eof()) { |
| 94 |
return ''; |
| 95 |
} |
| 96 |
$cipherText = ''; |
| 97 |
do { |
| 98 |
$cipherText .= $this->stream->read((int) ($length - \strlen($cipherText))); |
| 99 |
} while (\strlen($cipherText) < $length && !$this->stream->eof()); |
| 100 |
$options = \OPENSSL_RAW_DATA; |
| 101 |
if (!$this->stream->eof() && $this->stream->getSize() !== $this->stream->tell()) { |
| 102 |
$options |= \OPENSSL_ZERO_PADDING; |
| 103 |
} |
| 104 |
$plaintext = \openssl_decrypt($cipherText, $this->cipherMethod->getOpenSslName(), $this->key, $options, $this->cipherMethod->getCurrentIv()); |
| 105 |
$this->cipherMethod->update($cipherText); |
| 106 |
return $plaintext; |
| 107 |
} |
| 108 |
} |
| 109 |
|