| 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 encrypted with a passed cipher. |
| 11 |
*/ |
| 12 |
class AesEncryptingStream 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 $plainText |
| 35 |
* @param string $key |
| 36 |
* @param CipherMethod $cipherMethod |
| 37 |
*/ |
| 38 |
public function __construct(StreamInterface $plainText, $key, CipherMethod $cipherMethod) |
| 39 |
{ |
| 40 |
$this->stream = $plainText; |
| 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() : ?int |
| 57 |
{ |
| 58 |
$plainTextSize = $this->stream->getSize(); |
| 59 |
if ($this->cipherMethod->requiresPadding() && $plainTextSize !== null) { |
| 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. |
| 62 |
$padding = self::BLOCK_SIZE - $plainTextSize % self::BLOCK_SIZE; |
| 63 |
return $plainTextSize + $padding; |
| 64 |
} |
| 65 |
return $plainTextSize; |
| 66 |
} |
| 67 |
public function isWritable() : bool |
| 68 |
{ |
| 69 |
return \false; |
| 70 |
} |
| 71 |
public function read($length) : string |
| 72 |
{ |
| 73 |
if ($length > \strlen($this->buffer)) { |
| 74 |
$this->buffer .= $this->encryptBlock((int) self::BLOCK_SIZE * \ceil(($length - \strlen($this->buffer)) / self::BLOCK_SIZE)); |
| 75 |
} |
| 76 |
$data = \substr($this->buffer, 0, $length); |
| 77 |
$this->buffer = \substr($this->buffer, $length); |
| 78 |
return $data ? $data : ''; |
| 79 |
} |
| 80 |
public function seek($offset, $whence = \SEEK_SET) : void |
| 81 |
{ |
| 82 |
if ($whence === \SEEK_CUR) { |
| 83 |
$offset = $this->tell() + $offset; |
| 84 |
$whence = \SEEK_SET; |
| 85 |
} |
| 86 |
if ($whence === \SEEK_SET) { |
| 87 |
$this->buffer = ''; |
| 88 |
$wholeBlockOffset = (int) ($offset / self::BLOCK_SIZE) * self::BLOCK_SIZE; |
| 89 |
$this->stream->seek($wholeBlockOffset); |
| 90 |
$this->cipherMethod->seek($wholeBlockOffset); |
| 91 |
$this->read($offset - $wholeBlockOffset); |
| 92 |
} else { |
| 93 |
throw new LogicException('Unrecognized whence.'); |
| 94 |
} |
| 95 |
} |
| 96 |
private function encryptBlock($length) |
| 97 |
{ |
| 98 |
if ($this->stream->eof()) { |
| 99 |
return ''; |
| 100 |
} |
| 101 |
$plainText = ''; |
| 102 |
do { |
| 103 |
$plainText .= $this->stream->read((int) ($length - \strlen($plainText))); |
| 104 |
} while (\strlen($plainText) < $length && !$this->stream->eof()); |
| 105 |
$options = \OPENSSL_RAW_DATA; |
| 106 |
if (!$this->stream->eof() || $this->stream->getSize() !== $this->stream->tell()) { |
| 107 |
$options |= \OPENSSL_ZERO_PADDING; |
| 108 |
} |
| 109 |
$cipherText = \openssl_encrypt($plainText, $this->cipherMethod->getOpenSslName(), $this->key, $options, $this->cipherMethod->getCurrentIv()); |
| 110 |
$this->cipherMethod->update($cipherText); |
| 111 |
return $cipherText; |
| 112 |
} |
| 113 |
} |
| 114 |
|