PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 1ms app.codeStats 0ms

/public/packages/ckeditor/plugins/ckfinder/core/connector/php/vendor/aws/aws-sdk-php/src/Crypto/EncryptionTrait.php

https://bitbucket.org/AndreFigueira93/siscon-laravel
PHP | 186 lines | 119 code | 18 blank | 49 comment | 7 complexity | fbc8cf77f1432340864f8362134fc114 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, MPL-2.0-no-copyleft-exception
  1. <?php
  2. namespace Aws\Crypto;
  3. use GuzzleHttp\Psr7;
  4. use GuzzleHttp\Psr7\AppendStream;
  5. use GuzzleHttp\Psr7\Stream;
  6. trait EncryptionTrait
  7. {
  8. private static $allowedOptions = [
  9. 'Cipher' => true,
  10. 'KeySize' => true,
  11. 'Aad' => true,
  12. ];
  13. /**
  14. * Dependency to generate a CipherMethod from a set of inputs for loading
  15. * in to an AesEncryptingStream.
  16. *
  17. * @param string $cipherName Name of the cipher to generate for encrypting.
  18. * @param string $iv Base Initialization Vector for the cipher.
  19. * @param int $keySize Size of the encryption key, in bits, that will be
  20. * used.
  21. *
  22. * @return Cipher\CipherMethod
  23. *
  24. * @internal
  25. */
  26. abstract protected function buildCipherMethod($cipherName, $iv, $keySize);
  27. /**
  28. * Builds an AesStreamInterface and populates encryption metadata into the
  29. * supplied envelope.
  30. *
  31. * @param Stream $plaintext Plain-text data to be encrypted using the
  32. * materials, algorithm, and data provided.
  33. * @param array $cipherOptions Options for use in determining the cipher to
  34. * be used for encrypting data.
  35. * @param MaterialsProvider $provider A provider to supply and encrypt
  36. * materials used in encryption.
  37. * @param MetadataEnvelope $envelope A storage envelope for encryption
  38. * metadata to be added to.
  39. *
  40. * @return AesStreamInterface
  41. *
  42. * @throws \InvalidArgumentException Thrown when a value in $cipherOptions
  43. * is not valid.
  44. *
  45. * @internal
  46. */
  47. protected function encrypt(
  48. Stream $plaintext,
  49. array $cipherOptions,
  50. MaterialsProvider $provider,
  51. MetadataEnvelope $envelope
  52. ) {
  53. $materialsDescription = $provider->getMaterialsDescription();
  54. $cipherOptions = array_intersect_key(
  55. $cipherOptions,
  56. self::$allowedOptions
  57. );
  58. if (empty($cipherOptions['Cipher'])) {
  59. throw new \InvalidArgumentException('An encryption cipher must be'
  60. . ' specified in the "cipher_options".');
  61. }
  62. if (!self::isSupportedCipher($cipherOptions['Cipher'])) {
  63. throw new \InvalidArgumentException('The cipher requested is not'
  64. . ' supported by the SDK.');
  65. }
  66. if (empty($cipherOptions['KeySize'])) {
  67. $cipherOptions['KeySize'] = 256;
  68. }
  69. if (!is_int($cipherOptions['KeySize'])) {
  70. throw new \InvalidArgumentException('The cipher "KeySize" must be'
  71. . ' an integer.');
  72. }
  73. if (!MaterialsProvider::isSupportedKeySize(
  74. $cipherOptions['KeySize']
  75. )) {
  76. throw new \InvalidArgumentException('The cipher "KeySize" requested'
  77. . ' is not supported by AES (128, 192, or 256).');
  78. }
  79. $cipherOptions['Iv'] = $provider->generateIv(
  80. $this->getCipherOpenSslName(
  81. $cipherOptions['Cipher'],
  82. $cipherOptions['KeySize']
  83. )
  84. );
  85. $cek = $provider->generateCek($cipherOptions['KeySize']);
  86. list($encryptingStream, $aesName) = $this->getEncryptingStream(
  87. $plaintext,
  88. $cek,
  89. $cipherOptions
  90. );
  91. // Populate envelope data
  92. $envelope[MetadataEnvelope::CONTENT_KEY_V2_HEADER] =
  93. $provider->encryptCek(
  94. $cek,
  95. $materialsDescription
  96. );
  97. unset($cek);
  98. $envelope[MetadataEnvelope::IV_HEADER] =
  99. base64_encode($cipherOptions['Iv']);
  100. $envelope[MetadataEnvelope::KEY_WRAP_ALGORITHM_HEADER] =
  101. $provider->getWrapAlgorithmName();
  102. $envelope[MetadataEnvelope::CONTENT_CRYPTO_SCHEME_HEADER] = $aesName;
  103. $envelope[MetadataEnvelope::UNENCRYPTED_CONTENT_LENGTH_HEADER] =
  104. strlen($plaintext);
  105. $envelope[MetadataEnvelope::UNENCRYPTED_CONTENT_MD5_HEADER] =
  106. base64_encode(md5($plaintext));
  107. $envelope[MetadataEnvelope::MATERIALS_DESCRIPTION_HEADER] =
  108. json_encode($materialsDescription);
  109. if (!empty($cipherOptions['Tag'])) {
  110. $envelope[MetadataEnvelope::CRYPTO_TAG_LENGTH_HEADER] =
  111. strlen($cipherOptions['Tag']) * 8;
  112. }
  113. return $encryptingStream;
  114. }
  115. /**
  116. * Generates a stream that wraps the plaintext with the proper cipher and
  117. * uses the content encryption key (CEK) to encrypt the data when read.
  118. *
  119. * @param Stream $plaintext Plain-text data to be encrypted using the
  120. * materials, algorithm, and data provided.
  121. * @param string $cek A content encryption key for use by the stream for
  122. * encrypting the plaintext data.
  123. * @param array $cipherOptions Options for use in determining the cipher to
  124. * be used for encrypting data.
  125. *
  126. * @return [AesStreamInterface, string]
  127. *
  128. * @internal
  129. */
  130. protected function getEncryptingStream(
  131. Stream $plaintext,
  132. $cek,
  133. &$cipherOptions
  134. ) {
  135. switch ($cipherOptions['Cipher']) {
  136. case 'gcm':
  137. $cipherOptions['TagLength'] = 16;
  138. $cipherTextStream = new AesGcmEncryptingStream(
  139. $plaintext,
  140. $cek,
  141. $cipherOptions['Iv'],
  142. $cipherOptions['Aad'] = isset($cipherOptions['Aad'])
  143. ? $cipherOptions['Aad']
  144. : null,
  145. $cipherOptions['TagLength'],
  146. $cipherOptions['KeySize']
  147. );
  148. $appendStream = new AppendStream([
  149. $cipherTextStream->createStream()
  150. ]);
  151. $cipherOptions['Tag'] = $cipherTextStream->getTag();
  152. $appendStream->addStream(Psr7\stream_for($cipherOptions['Tag']));
  153. return [$appendStream, $cipherTextStream->getAesName()];
  154. default:
  155. $cipherMethod = $this->buildCipherMethod(
  156. $cipherOptions['Cipher'],
  157. $cipherOptions['Iv'],
  158. $cipherOptions['KeySize']
  159. );
  160. $cipherTextStream = new AesEncryptingStream(
  161. $plaintext,
  162. $cek,
  163. $cipherMethod
  164. );
  165. return [$cipherTextStream, $cipherTextStream->getAesName()];
  166. }
  167. }
  168. }