Debian Codegraph @

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

raw symbols SCIP
1 <?php
2 namespace Aws\S3\Crypto;
3
4 use Aws\Crypto\DecryptionTrait;
5 use Aws\HashingStream;
6 use Aws\MetricsBuilder;
7 use Aws\PhpHash;
8 use Aws\Crypto\AbstractCryptoClient;
9 use Aws\Crypto\EncryptionTrait;
10 use Aws\Crypto\MetadataEnvelope;
11 use Aws\Crypto\MaterialsProvider;
12 use Aws\Crypto\Cipher\CipherBuilderTrait;
13 use Aws\S3\S3Client;
14 use GuzzleHttp\Promise;
15 use GuzzleHttp\Promise\PromiseInterface;
16 use GuzzleHttp\Psr7;
17
18 /**
19 * Provides a wrapper for an S3Client that supplies functionality to encrypt
20 * data on putObject[Async] calls and decrypt data on getObject[Async] calls.
21 *
22 * Legacy implementation using older encryption workflow.
23 *
24 * AWS strongly recommends the upgrade to the S3EncryptionClientV2 (over the
25 * S3EncryptionClient), as it offers updated data security best practices to our
26 * customers who upgrade. S3EncryptionClientV2 contains breaking changes, so this
27 * will require planning by engineering teams to migrate. New workflows should
28 * just start with S3EncryptionClientV2.
29 *
30 * @deprecated
31 */
32 class S3EncryptionClient extends AbstractCryptoClient
33 {
34 use CipherBuilderTrait;
35 use CryptoParamsTrait;
36 use DecryptionTrait;
37 use EncryptionTrait;
38 use UserAgentTrait;
39
40 const CRYPTO_VERSION = '1n';
41
42 private $client;
43 private $instructionFileSuffix;
44
45 /**
46 * @param S3Client $client The S3Client to be used for true uploading and
47 * retrieving objects from S3 when using the
48 * encryption client.
49 * @param string|null $instructionFileSuffix Suffix for a client wide
50 * default when using instruction
51 * files for metadata storage.
52 */
53 public function __construct(
54 S3Client $client,
55 $instructionFileSuffix = null
56 ) {
57 trigger_error(
58 'S3EncryptionClient is deprecated and will be removed in a future ' .
59 'release due to security vulnerabilities. Please migrate to ' .
60 'S3EncryptionClientV3 as soon as possible.' . "\n" .
61 'See https://docs.aws.amazon.com/sdk-for-php/v3/developer-guide/' .
62 'security.html for upgrade guidance.',
63 E_USER_DEPRECATED
64 );
65 $this->client = $client;
66 $this->instructionFileSuffix = $instructionFileSuffix;
67 MetricsBuilder::appendMetricsCaptureMiddleware(
68 $this->client->getHandlerList(),
69 MetricsBuilder::S3_CRYPTO_V1N
70 );
71 }
72
73 private static function getDefaultStrategy()
74 {
75 return new HeadersMetadataStrategy();
76 }
77
78 /**
79 * Encrypts the data in the 'Body' field of $args and promises to upload it
80 * to the specified location on S3.
81 *
82 * @param array $args Arguments for encrypting an object and uploading it
83 * to S3 via PutObject.
84 *
85 * The required configuration arguments are as follows:
86 *
87 * - @MaterialsProvider: (MaterialsProvider) Provides Cek, Iv, and Cek
88 * encrypting/decrypting for encryption metadata.
89 * - @CipherOptions: (array) Cipher options for encrypting data. Only the
90 * Cipher option is required. Accepts the following:
91 * - Cipher: (string) cbc|gcm
92 * See also: AbstractCryptoClient::$supportedCiphers. Note that
93 * cbc is deprecated and gcm should be used when possible.
94 * - KeySize: (int) 128|192|256
95 * See also: MaterialsProvider::$supportedKeySizes
96 * - Aad: (string) Additional authentication data. This option is
97 * passed directly to OpenSSL when using gcm. It is ignored when
98 * using cbc. Note if you pass in Aad for gcm encryption, the
99 * PHP SDK will be able to decrypt the resulting object, but other
100 * AWS SDKs may not be able to do so.
101 *
102 * The optional configuration arguments are as follows:
103 *
104 * - @MetadataStrategy: (MetadataStrategy|string|null) Strategy for storing
105 * MetadataEnvelope information. Defaults to using a
106 * HeadersMetadataStrategy. Can either be a class implementing
107 * MetadataStrategy, a class name of a predefined strategy, or empty/null
108 * to default.
109 * - @InstructionFileSuffix: (string|null) Suffix used when writing to an
110 * instruction file if using an InstructionFileMetadataHandler.
111 *
112 * @return PromiseInterface
113 *
114 * @throws \InvalidArgumentException Thrown when arguments above are not
115 * passed or are passed incorrectly.
116 */
117 public function putObjectAsync(array $args)
118 {
119 $provider = $this->getMaterialsProvider($args);
120 unset($args['@MaterialsProvider']);
121
122 $instructionFileSuffix = $this->getInstructionFileSuffix($args);
123 unset($args['@InstructionFileSuffix']);
124
125 $strategy = $this->getMetadataStrategy($args, $instructionFileSuffix);
126 unset($args['@MetadataStrategy']);
127
128 $envelope = new MetadataEnvelope();
129
130 return Promise\Create::promiseFor($this->encrypt(
131 Psr7\Utils::streamFor($args['Body']),
132 $args['@CipherOptions'] ?: [],
133 $provider,
134 $envelope
135 ))->then(
136 function ($encryptedBodyStream) use ($args) {
137 $hash = new PhpHash('sha256');
138 $hashingEncryptedBodyStream = new HashingStream(
139 $encryptedBodyStream,
140 $hash,
141 self::getContentShaDecorator($args)
142 );
143 return [$hashingEncryptedBodyStream, $args];
144 }
145 )->then(
146 function ($putObjectContents) use ($strategy, $envelope) {
147 list($bodyStream, $args) = $putObjectContents;
148 if ($strategy === null) {
149 $strategy = self::getDefaultStrategy();
150 }
151
152 $updatedArgs = $strategy->save($envelope, $args);
153 $updatedArgs['Body'] = $bodyStream;
154 return $updatedArgs;
155 }
156 )->then(
157 function ($args) {
158 unset($args['@CipherOptions']);
159 return $this->client->putObjectAsync($args);
160 }
161 );
162 }
163
164 private static function getContentShaDecorator(&$args)
165 {
166 return function ($hash) use (&$args) {
167 $args['ContentSHA256'] = bin2hex($hash);
168 };
169 }
170
171 /**
172 * Encrypts the data in the 'Body' field of $args and uploads it to the
173 * specified location on S3.
174 *
175 * @param array $args Arguments for encrypting an object and uploading it
176 * to S3 via PutObject.
177 *
178 * The required configuration arguments are as follows:
179 *
180 * - @MaterialsProvider: (MaterialsProvider) Provides Cek, Iv, and Cek
181 * encrypting/decrypting for encryption metadata.
182 * - @CipherOptions: (array) Cipher options for encrypting data. A Cipher
183 * is required. Accepts the following options:
184 * - Cipher: (string) cbc|gcm
185 * See also: AbstractCryptoClient::$supportedCiphers. Note that
186 * cbc is deprecated and gcm should be used when possible.
187 * - KeySize: (int) 128|192|256
188 * See also: MaterialsProvider::$supportedKeySizes
189 * - Aad: (string) Additional authentication data. This option is
190 * passed directly to OpenSSL when using gcm. It is ignored when
191 * using cbc. Note if you pass in Aad for gcm encryption, the
192 * PHP SDK will be able to decrypt the resulting object, but other
193 * AWS SDKs may not be able to do so.
194 *
195 * The optional configuration arguments are as follows:
196 *
197 * - @MetadataStrategy: (MetadataStrategy|string|null) Strategy for storing
198 * MetadataEnvelope information. Defaults to using a
199 * HeadersMetadataStrategy. Can either be a class implementing
200 * MetadataStrategy, a class name of a predefined strategy, or empty/null
201 * to default.
202 * - @InstructionFileSuffix: (string|null) Suffix used when writing to an
203 * instruction file if an using an InstructionFileMetadataHandler was
204 * determined.
205 *
206 * @return \Aws\Result PutObject call result with the details of uploading
207 * the encrypted file.
208 *
209 * @throws \InvalidArgumentException Thrown when arguments above are not
210 * passed or are passed incorrectly.
211 */
212 public function putObject(array $args)
213 {
214 return $this->putObjectAsync($args)->wait();
215 }
216
217 /**
218 * Promises to retrieve an object from S3 and decrypt the data in the
219 * 'Body' field.
220 *
221 * @param array $args Arguments for retrieving an object from S3 via
222 * GetObject and decrypting it.
223 *
224 * The required configuration argument is as follows:
225 *
226 * - @MaterialsProvider: (MaterialsProvider) Provides Cek, Iv, and Cek
227 * encrypting/decrypting for decryption metadata. May have data loaded
228 * from the MetadataEnvelope upon decryption.
229 *
230 * The optional configuration arguments are as follows:
231 *
232 * - SaveAs: (string) The path to a file on disk to save the decrypted
233 * object data. This will be handled by file_put_contents instead of the
234 * Guzzle sink.
235 *
236 * - @MetadataStrategy: (MetadataStrategy|string|null) Strategy for reading
237 * MetadataEnvelope information. Defaults to determining based on object
238 * response headers. Can either be a class implementing MetadataStrategy,
239 * a class name of a predefined strategy, or empty/null to default.
240 * - @InstructionFileSuffix: (string) Suffix used when looking for an
241 * instruction file if an InstructionFileMetadataHandler is being used.
242 * - @CipherOptions: (array) Cipher options for decrypting data. A Cipher
243 * is required. Accepts the following options:
244 * - Aad: (string) Additional authentication data. This option is
245 * passed directly to OpenSSL when using gcm. It is ignored when
246 * using cbc.
247 *
248 * @return PromiseInterface
249 *
250 * @throws \InvalidArgumentException Thrown when required arguments are not
251 * passed or are passed incorrectly.
252 */
253 public function getObjectAsync(array $args)
254 {
255 $provider = $this->getMaterialsProvider($args);
256 unset($args['@MaterialsProvider']);
257
258 $instructionFileSuffix = $this->getInstructionFileSuffix($args);
259 unset($args['@InstructionFileSuffix']);
260
261 $strategy = $this->getMetadataStrategy($args, $instructionFileSuffix);
262 unset($args['@MetadataStrategy']);
263
264 $saveAs = null;
265 if (!empty($args['SaveAs'])) {
266 $saveAs = $args['SaveAs'];
267 }
268
269 $promise = $this->client->getObjectAsync($args)
270 ->then(
271 function ($result) use (
272 $provider,
273 $instructionFileSuffix,
274 $strategy,
275 $args
276 ) {
277 if ($strategy === null) {
278 $strategy = $this->determineGetObjectStrategy(
279 $result,
280 $instructionFileSuffix
281 );
282 }
283
284 $envelope = $strategy->load($args + [
285 'Metadata' => $result['Metadata']
286 ]);
287
288 $provider = $provider->fromDecryptionEnvelope($envelope);
289
290 $result['Body'] = $this->decrypt(
291 $result['Body'],
292 $provider,
293 $envelope,
294 isset($args['@CipherOptions'])
295 ? $args['@CipherOptions']
296 : []
297 );
298 return $result;
299 }
300 )->then(
301 function ($result) use ($saveAs) {
302 if (!empty($saveAs)) {
303 file_put_contents(
304 $saveAs,
305 (string)$result['Body'],
306 LOCK_EX
307 );
308 }
309 return $result;
310 }
311 );
312
313 return $promise;
314 }
315
316 /**
317 * Retrieves an object from S3 and decrypts the data in the 'Body' field.
318 *
319 * @param array $args Arguments for retrieving an object from S3 via
320 * GetObject and decrypting it.
321 *
322 * The required configuration argument is as follows:
323 *
324 * - @MaterialsProvider: (MaterialsProvider) Provides Cek, Iv, and Cek
325 * encrypting/decrypting for decryption metadata. May have data loaded
326 * from the MetadataEnvelope upon decryption.
327 *
328 * The optional configuration arguments are as follows:
329 *
330 * - SaveAs: (string) The path to a file on disk to save the decrypted
331 * object data. This will be handled by file_put_contents instead of the
332 * Guzzle sink.
333 * - @InstructionFileSuffix: (string|null) Suffix used when looking for an
334 * instruction file if an InstructionFileMetadataHandler was detected.
335 * - @CipherOptions: (array) Cipher options for encrypting data. A Cipher
336 * is required. Accepts the following options:
337 * - Aad: (string) Additional authentication data. This option is
338 * passed directly to OpenSSL when using gcm. It is ignored when
339 * using cbc.
340 *
341 * @return \Aws\Result GetObject call result with the 'Body' field
342 * wrapped in a decryption stream with its metadata
343 * information.
344 *
345 * @throws \InvalidArgumentException Thrown when arguments above are not
346 * passed or are passed incorrectly.
347 */
348 public function getObject(array $args)
349 {
350 return $this->getObjectAsync($args)->wait();
351 }
352 }