AesGcmDecryptingStream.php 2.7 KB

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