| 1 |
<?php |
| 2 |
namespace Aws\Crypto; |
| 3 |
|
| 4 |
use Aws\Exception\CryptoException; |
| 5 |
use GuzzleHttp\Psr7; |
| 6 |
use GuzzleHttp\Psr7\StreamDecoratorTrait; |
| 7 |
use Psr\Http\Message\StreamInterface; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class AesGcmDecryptingStream implements AesStreamInterface |
| 13 |
{ |
| 14 |
use StreamDecoratorTrait; |
| 15 |
|
| 16 |
private $aad; |
| 17 |
|
| 18 |
private $initializationVector; |
| 19 |
|
| 20 |
private $key; |
| 21 |
|
| 22 |
private $keySize; |
| 23 |
|
| 24 |
private $cipherText; |
| 25 |
|
| 26 |
private $tag; |
| 27 |
|
| 28 |
private $tagLength; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
private $stream; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public function __construct( |
| 45 |
StreamInterface $cipherText, |
| 46 |
$key, |
| 47 |
$initializationVector, |
| 48 |
$tag, |
| 49 |
$aad = '', |
| 50 |
$tagLength = 128, |
| 51 |
$keySize = 256 |
| 52 |
) { |
| 53 |
$this->cipherText = $cipherText; |
| 54 |
$this->key = $key; |
| 55 |
$this->initializationVector = $initializationVector; |
| 56 |
$this->tag = $tag; |
| 57 |
$this->aad = $aad; |
| 58 |
$this->tagLength = $tagLength; |
| 59 |
$this->keySize = $keySize; |
| 60 |
|
| 61 |
|
| 62 |
unset($this->stream); |
| 63 |
} |
| 64 |
|
| 65 |
public function getOpenSslName() |
| 66 |
{ |
| 67 |
return "aes-{$this->keySize}-gcm"; |
| 68 |
} |
| 69 |
|
| 70 |
public function getAesName() |
| 71 |
{ |
| 72 |
return 'AES/GCM/NoPadding'; |
| 73 |
} |
| 74 |
|
| 75 |
public function getCurrentIv() |
| 76 |
{ |
| 77 |
return $this->initializationVector; |
| 78 |
} |
| 79 |
|
| 80 |
public function createStream() |
| 81 |
{ |
| 82 |
|
| 83 |
$result = \openssl_decrypt( |
| 84 |
(string)$this->cipherText, |
| 85 |
$this->getOpenSslName(), |
| 86 |
$this->key, |
| 87 |
OPENSSL_RAW_DATA, |
| 88 |
$this->initializationVector, |
| 89 |
$this->tag, |
| 90 |
$this->aad |
| 91 |
); |
| 92 |
if ($result === false) { |
| 93 |
throw new CryptoException('The requested object could not be ' |
| 94 |
. 'decrypted due to an invalid authentication tag.'); |
| 95 |
} |
| 96 |
return Psr7\Utils::streamFor($result); |
| 97 |
|
| 98 |
} |
| 99 |
|
| 100 |
public function isWritable(): bool |
| 101 |
{ |
| 102 |
return false; |
| 103 |
} |
| 104 |
} |