Debian Codegraph @

lib/3rdParty/composer/aws/aws-sdk-php/src/Crypto/EncryptionTraitV2.php

raw symbols SCIP
1 <?php
2 namespace Aws\Crypto;
3
4 use GuzzleHttp\Psr7;
5 use GuzzleHttp\Psr7\AppendStream;
6 use GuzzleHttp\Psr7\Stream;
7 use Psr\Http\Message\StreamInterface;
8
9 trait EncryptionTraitV2
10 {
11 private static $allowedOptions = [
12 'Cipher' => true,
13 'KeySize' => true,
14 'Aad' => true,
15 ];
16
17 private static $encryptClasses = [
18 'gcm' => AesGcmEncryptingStream::class
19 ];
20
21 /**
22 * Dependency to generate a CipherMethod from a set of inputs for loading
23 * in to an AesEncryptingStream.
24 *
25 * @param string $cipherName Name of the cipher to generate for encrypting.
26 * @param string $iv Base Initialization Vector for the cipher.
27 * @param int $keySize Size of the encryption key, in bits, that will be
28 * used.
29 *
30 * @return Cipher\CipherMethod
31 *
32 * @internal
33 */
34 abstract protected function buildCipherMethod($cipherName, $iv, $keySize);
35
36 /**
37 * Builds an AesStreamInterface and populates encryption metadata into the
38 * supplied envelope.
39 *
40 * @param Stream $plaintext Plain-text data to be encrypted using the
41 * materials, algorithm, and data provided.
42 * @param array $options Options for use in encryption, including cipher
43 * options, and encryption context.
44 * @param MaterialsProviderV2 $provider A provider to supply and encrypt
45 * materials used in encryption.
46 * @param MetadataEnvelope $envelope A storage envelope for encryption
47 * metadata to be added to.
48 *
49 * @return StreamInterface
50 *
51 * @throws \InvalidArgumentException Thrown when a value in $options['@CipherOptions']
52 * is not valid.
53 *s
54 * @internal
55 */
56 public function encrypt(
57 Stream $plaintext,
58 array $options,
59 MaterialsProviderV2 $provider,
60 MetadataEnvelope $envelope
61 ) {
62 $options = array_change_key_case($options);
63 $cipherOptions = array_intersect_key(
64 $options['@cipheroptions'],
65 self::$allowedOptions
66 );
67
68 if (empty($cipherOptions['Cipher'])) {
69 throw new \InvalidArgumentException('An encryption cipher must be'
70 . ' specified in @CipherOptions["Cipher"].');
71 }
72
73 $cipherOptions['Cipher'] = strtolower($cipherOptions['Cipher']);
74
75 if (!self::isSupportedCipher($cipherOptions['Cipher'])) {
76 throw new \InvalidArgumentException('The cipher requested is not'
77 . ' supported by the SDK.');
78 }
79
80 if (empty($cipherOptions['KeySize'])) {
81 $cipherOptions['KeySize'] = 256;
82 }
83 if (!is_int($cipherOptions['KeySize'])) {
84 throw new \InvalidArgumentException('The cipher "KeySize" must be'
85 . ' an integer.');
86 }
87
88 if (!MaterialsProviderV2::isSupportedKeySize($cipherOptions['KeySize'])) {
89 throw new \InvalidArgumentException('The cipher "KeySize" requested'
90 . ' is not supported by AES (128 or 256).');
91 }
92
93 $cipherOptions['Iv'] = $provider->generateIv(
94 $this->getCipherOpenSslName(
95 $cipherOptions['Cipher'],
96 $cipherOptions['KeySize']
97 )
98 );
99
100 $encryptClass = self::$encryptClasses[$cipherOptions['Cipher']];
101 $aesName = $encryptClass::getStaticAesName();
102 $materialsDescription = ['aws:x-amz-cek-alg' => $aesName];
103
104 $keys = $provider->generateCek(
105 $cipherOptions['KeySize'],
106 $materialsDescription,
107 $options
108 );
109
110 // Some providers modify materials description based on options
111 if (isset($keys['UpdatedContext'])) {
112 $materialsDescription = $keys['UpdatedContext'];
113 }
114
115 $encryptingStream = $this->getEncryptingStream(
116 $plaintext,
117 $keys['Plaintext'],
118 $cipherOptions
119 );
120
121 // Populate envelope data
122 $envelope[MetadataEnvelope::CONTENT_KEY_V2_HEADER] = $keys['Ciphertext'];
123 unset($keys);
124
125 $envelope[MetadataEnvelope::IV_HEADER] =
126 base64_encode($cipherOptions['Iv']);
127 $envelope[MetadataEnvelope::KEY_WRAP_ALGORITHM_HEADER] =
128 $provider->getWrapAlgorithmName();
129 $envelope[MetadataEnvelope::CONTENT_CRYPTO_SCHEME_HEADER] = $aesName;
130 $envelope[MetadataEnvelope::UNENCRYPTED_CONTENT_LENGTH_HEADER] =
131 (string) strlen($plaintext);
132 $envelope[MetadataEnvelope::MATERIALS_DESCRIPTION_HEADER] =
133 json_encode($materialsDescription);
134 if (!empty($cipherOptions['Tag'])) {
135 $envelope[MetadataEnvelope::CRYPTO_TAG_LENGTH_HEADER] =
136 (string) (strlen($cipherOptions['Tag']) * 8);
137 }
138
139 return $encryptingStream;
140 }
141
142 /**
143 * Generates a stream that wraps the plaintext with the proper cipher and
144 * uses the content encryption key (CEK) to encrypt the data when read.
145 *
146 * @param Stream $plaintext Plain-text data to be encrypted using the
147 * materials, algorithm, and data provided.
148 * @param string $cek A content encryption key for use by the stream for
149 * encrypting the plaintext data.
150 * @param array $cipherOptions Options for use in determining the cipher to
151 * be used for encrypting data.
152 *
153 * @return array returns an array with two elements as follows: [string, AesStreamInterface]
154 *
155 * @internal
156 */
157 protected function getEncryptingStream(
158 Stream $plaintext,
159 $cek,
160 &$cipherOptions
161 ) {
162 switch ($cipherOptions['Cipher']) {
163 // Only 'gcm' is supported for encryption currently
164 case 'gcm':
165 $cipherOptions['TagLength'] = 16;
166 $encryptClass = self::$encryptClasses['gcm'];
167 $cipherTextStream = new $encryptClass(
168 $plaintext,
169 $cek,
170 $cipherOptions['Iv'],
171 $cipherOptions['Aad'] = $cipherOptions['Aad'] ?? '',
172 $cipherOptions['TagLength'],
173 $cipherOptions['KeySize']
174 );
175
176 if (!empty($cipherOptions['Aad'])) {
177 trigger_error("'Aad' has been supplied for content encryption"
178 . " with " . $cipherTextStream->getAesName() . ". The"
179 . " PHP SDK encryption client can decrypt an object"
180 . " encrypted in this way, but other AWS SDKs may not be"
181 . " able to.", E_USER_WARNING);
182 }
183
184 $appendStream = new AppendStream([
185 $cipherTextStream->createStream()
186 ]);
187 $cipherOptions['Tag'] = $cipherTextStream->getTag();
188 $appendStream->addStream(Psr7\Utils::streamFor($cipherOptions['Tag']));
189 return $appendStream;
190 }
191 }
192 }