| 1 | <?php |
| 2 | namespace Aws\S3\Crypto; |
| 3 | |
| 4 | use Aws\Crypto\DecryptionTraitV3; |
| 5 | use Aws\Exception\CryptoException; |
| 6 | use Aws\HashingStream; |
| 7 | use Aws\MetricsBuilder; |
| 8 | use Aws\PhpHash; |
| 9 | use Aws\Result; |
| 10 | use Aws\Crypto\AbstractCryptoClientV3; |
| 11 | use Aws\Crypto\EncryptionTraitV3; |
| 12 | use Aws\Crypto\MetadataEnvelope; |
| 13 | use Aws\Crypto\AlgorithmSuite; |
| 14 | use Aws\Crypto\Cipher\CipherBuilderTrait; |
| 15 | use Aws\S3\S3Client; |
| 16 | use GuzzleHttp\Promise; |
| 17 | use GuzzleHttp\Promise\PromiseInterface; |
| 18 | use GuzzleHttp\Psr7; |
| 19 | |
| 20 | /** |
| 21 | * Provides a wrapper for an S3Client that supplies functionality to encrypt |
| 22 | * data on putObject[Async] calls and decrypt data on getObject[Async] calls. |
| 23 | * |
| 24 | * AWS strongly recommends the upgrade to the S3EncryptionClientV3 (over the |
| 25 | * S3EncryptionClientV2 or S3EncryptionClient), as it offers updated |
| 26 | * data security best practices to our customers who upgrade. |
| 27 | * S3EncryptionClientV3 contains breaking changes, so this |
| 28 | * will require planning by engineering teams to migrate. New workflows should |
| 29 | * just start with S3EncryptionClientV3. |
| 30 | * |
| 31 | * |
| 32 | * Example write path: |
| 33 | * |
| 34 | * <code> |
| 35 | * use Aws\Crypto\KmsMaterialsProviderV3; |
| 36 | * use Aws\S3\Crypto\S3EncryptionClientV3; |
| 37 | * use Aws\S3\S3Client; |
| 38 | * |
| 39 | * $encryptionClient = new S3EncryptionClientV3( |
| 40 | * new S3Client([ |
| 41 | * 'region' => 'us-west-2', |
| 42 | * 'version' => 'latest' |
| 43 | * ]) |
| 44 | * ); |
| 45 | * $materialsProvider = new KmsMaterialsProviderV3( |
| 46 | * new KmsClient([ |
| 47 | * 'profile' => 'default', |
| 48 | * 'region' => 'us-east-1', |
| 49 | * 'version' => 'latest', |
| 50 | * ], |
| 51 | * 'your-kms-key-id' |
| 52 | * ); |
| 53 | * |
| 54 | * $encryptionClient->putObject([ |
| 55 | * '@MaterialsProvider' => $materialsProvider, |
| 56 | * '@CipherOptions' => [ |
| 57 | * 'Cipher' => 'gcm', |
| 58 | * 'KeySize' => 256, |
| 59 | * ], |
| 60 | * '@CommitmentPolicy' => 'REQUIRE_ENCRYPT_REQUIRE_DECRYPT', |
| 61 | * '@KmsEncryptionContext' => ['foo' => 'bar'], |
| 62 | * 'Bucket' => 'your-bucket', |
| 63 | * 'Key' => 'your-key', |
| 64 | * 'Body' => 'your-encrypted-data', |
| 65 | * ]); |
| 66 | * </code> |
| 67 | * |
| 68 | * Example read call (using objects from previous example): |
| 69 | * |
| 70 | * <code> |
| 71 | * $encryptionClient->getObject([ |
| 72 | * '@MaterialsProvider' => $materialsProvider, |
| 73 | * '@CipherOptions' => [ |
| 74 | * 'Cipher' => 'gcm', |
| 75 | * 'KeySize' => 256, |
| 76 | * ], |
| 77 | * '@CommitmentPolicy' => 'REQUIRE_ENCRYPT_REQUIRE_DECRYPT', |
| 78 | * 'Bucket' => 'your-bucket', |
| 79 | * 'Key' => 'your-key', |
| 80 | * ]); |
| 81 | * </code> |
| 82 | */ |
| 83 | class S3EncryptionClientV3 extends AbstractCryptoClientV3 |
| 84 | { |
| 85 | use CipherBuilderTrait; |
| 86 | use CryptoParamsTraitV3; |
| 87 | use DecryptionTraitV3; |
| 88 | use EncryptionTraitV3; |
| 89 | use UserAgentTrait; |
| 90 | |
| 91 | const CRYPTO_VERSION = '3.0'; |
| 92 | |
| 93 | private S3Client $client; |
| 94 | private ?string $instructionFileSuffix; |
| 95 | private int $legacyWarningCount; |
| 96 | |
| 97 | /** |
| 98 | * @param S3Client $client The S3Client to be used for true uploading and |
| 99 | * retrieving objects from S3 when using the |
| 100 | * encryption client. |
| 101 | * @param string|null $instructionFileSuffix Suffix for a client wide |
| 102 | * default when using instruction |
| 103 | * files for metadata storage. |
| 104 | */ |
| 105 | public function __construct( |
| 106 | //= ../specification/s3-encryption/client.md#wrapped-s3-client-s |
| 107 | //= type=implication |
| 108 | //# The S3EC MUST support the option to provide an SDK S3 client instance during its initialization. |
| 109 | S3Client $client, |
| 110 | //= ../specification/s3-encryption/client.md#aws-sdk-compatibility |
| 111 | //= type=implication |
| 112 | //# The S3EC MUST provide a different set of configuration options than the conventional S3 client. |
| 113 | |
| 114 | //= ../specification/s3-encryption/client.md#instruction-file-configuration |
| 115 | //= type=implication |
| 116 | //# In this case, the Instruction File Configuration SHOULD be optional, |
| 117 | //# such that its default configuration is used when none is provided. |
| 118 | ?string $instructionFileSuffix = null |
| 119 | ) { |
| 120 | //= ../specification/s3-encryption/client.md#aws-sdk-compatibility |
| 121 | //= type=implication |
| 122 | //# The S3EC MUST adhere to the same interface for API operations as the conventional AWS SDK S3 client. |
| 123 | $this->appendUserAgent($client, 'feat/s3-encrypt-php/' . self::CRYPTO_VERSION); |
| 124 | //= ../specification/s3-encryption/client.md#wrapped-s3-client-s |
| 125 | //# The S3EC MUST NOT support use of S3EC as the provided S3 client during its initialization; |
| 126 | //# it MUST throw an exception in this case. |
| 127 | if ($client instanceof S3EncryptionClientV3) { |
| 128 | throw new CryptoException("Client configuration error." |
| 129 | . " An S3 Encryption Client is not a valid S3 client for an S3 Encryption Client."); |
| 130 | } |
| 131 | $this->client = $client; |
| 132 | //= ../specification/s3-encryption/client.md#instruction-file-configuration |
| 133 | //= type=implication |
| 134 | //# The S3EC MAY support the option to provide Instruction File Configuration during its initialization. |
| 135 | |
| 136 | //= ../specification/s3-encryption/client.md#instruction-file-configuration |
| 137 | //= type=implication |
| 138 | //# If the S3EC in a given language supports Instruction Files, |
| 139 | //# then it MUST accept Instruction File Configuration during its initialization. |
| 140 | $this->instructionFileSuffix = $instructionFileSuffix; |
| 141 | $this->legacyWarningCount = 0; |
| 142 | MetricsBuilder::appendMetricsCaptureMiddleware( |
| 143 | $this->client->getHandlerList(), |
| 144 | MetricsBuilder::S3_CRYPTO_V3 |
| 145 | ); |
| 146 | |
| 147 | if (!extension_loaded('openssl')) { |
| 148 | throw new CryptoException("Unable to load `openssl` extension."); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | //= ../specification/s3-encryption/data-format/metadata-strategy.md#instruction-file |
| 153 | //# Instruction File writes MUST NOT be enabled by default. |
| 154 | private static function getDefaultStrategy(): HeadersMetadataStrategy |
| 155 | { |
| 156 | return new HeadersMetadataStrategy(); |
| 157 | } |
| 158 | |
| 159 | /** |
| 160 | * Encrypts the data in the 'Body' field of $args and promises to upload it |
| 161 | * to the specified location on S3. |
| 162 | * |
| 163 | * @param array $args Arguments for encrypting an object and uploading it |
| 164 | * to S3 via PutObject. |
| 165 | * |
| 166 | * The required configuration arguments are as follows: |
| 167 | * |
| 168 | * - @MaterialsProvider: (MaterialsProviderV3) Provides Cek, Iv, and Cek |
| 169 | * encrypting/decrypting for encryption metadata. |
| 170 | * - @CommitmentPolicy: (string) Must be set to 'FORBID_ENCRYPT_ALLOW_DECRYPT', |
| 171 | * 'REQUIRE_ENCRYPT_ALLOW_DECRYPT', or 'REQUIRE_ENCRYPT_REQUIRE_DECRYPT'. |
| 172 | * - 'FORBID_ENCRYPT_ALLOW_DECRYPT' indicates that the client is configured |
| 173 | * to write messages without key commitment and read messages encrypted |
| 174 | * with key commitment or without key commitment. |
| 175 | * - 'REQUIRE_ENCRYPT_ALLOW_DECRYPT' indicates that the client is configured |
| 176 | * to write messages with key commitment and read messages encrypted |
| 177 | * with key commitment or without key commitment. |
| 178 | * - 'REQUIRE_ENCRYPT_REQUIRE_DECRYPT' indicates that the client is configured |
| 179 | * to write messages with key commitment and read messages encrypted |
| 180 | * with key commitment. |
| 181 | * - @CipherOptions: (array) Cipher options for encrypting data. Only the |
| 182 | * Cipher option is required. Accepts the following: |
| 183 | * - Cipher: (string) gcm |
| 184 | * See also: AbstractCryptoClientV3::$supportedCiphers |
| 185 | * - KeySize: (int) 256 |
| 186 | * See also: MaterialsProvider::$supportedKeySizes |
| 187 | * - Aad: (string) Additional authentication data. This option is |
| 188 | * passed directly to OpenSSL when using gcm. Note if you pass in |
| 189 | * Aad, the PHP SDK will be able to decrypt the resulting object, |
| 190 | * but other AWS SDKs may not be able to do so. |
| 191 | * - @KmsEncryptionContext: (array) Only required if using |
| 192 | * KmsMaterialsProviderV3. An associative array of key-value |
| 193 | * pairs to be added to the encryption context for KMS key encryption. An |
| 194 | * empty array may be passed if no additional context is desired. |
| 195 | * |
| 196 | * The optional configuration arguments are as follows: |
| 197 | * |
| 198 | * - @MetadataStrategy: (MetadataStrategy|string|null) Strategy for storing |
| 199 | * MetadataEnvelope information. Defaults to using a |
| 200 | * HeadersMetadataStrategy. Can either be a class implementing |
| 201 | * MetadataStrategy, a class name of a predefined strategy, or empty/null |
| 202 | * to default. |
| 203 | * - @InstructionFileSuffix: (string|null) Suffix used when writing to an |
| 204 | * instruction file if using an InstructionFileMetadataHandler. |
| 205 | * |
| 206 | * @return PromiseInterface |
| 207 | * |
| 208 | * @throws \InvalidArgumentException Thrown when arguments above are not |
| 209 | * passed or are passed incorrectly. |
| 210 | */ |
| 211 | public function putObjectAsync(array $args): PromiseInterface |
| 212 | { |
| 213 | //= ../specification/s3-encryption/client.md#cryptographic-materials |
| 214 | //= type=implication |
| 215 | //# The S3EC MUST accept either one CMM or one Keyring instance upon initialization. |
| 216 | $provider = $this->getMaterialsProvider($args); |
| 217 | unset($args['@MaterialsProvider']); |
| 218 | |
| 219 | //= ../specification/s3-encryption/client.md#key-commitment |
| 220 | //= type=implication |
| 221 | //# The S3EC MUST support configuration of the [Key Commitment policy](./key-commitment.md) during its initialization. |
| 222 | $keyCommitmentPolicy = $this->getKeyCommitmentPolicy($args); |
| 223 | unset($args['@CommitmentPolicy']); |
| 224 | |
| 225 | //= ../specification/s3-encryption/data-format/metadata-strategy.md#instruction-file |
| 226 | //# Instruction File writes MUST be optionally configured during client creation or on each PutObject request. |
| 227 | $instructionFileSuffix = $this->getInstructionFileSuffix($args); |
| 228 | unset($args['@InstructionFileSuffix']); |
| 229 | |
| 230 | $strategy = $this->getMetadataStrategy($args, $instructionFileSuffix); |
| 231 | unset($args['@MetadataStrategy']); |
| 232 | |
| 233 | //= ../specification/s3-encryption/client.md#encryption-algorithm |
| 234 | //= type=implication |
| 235 | //# The S3EC MUST support configuration of the encryption algorithm (or algorithm suite) during its initialization. |
| 236 | $options = array_change_key_case($args); |
| 237 | $cipherOptions = array_intersect_key( |
| 238 | $options['@cipheroptions'], |
| 239 | self::$allowedOptions |
| 240 | ); |
| 241 | if (empty($cipherOptions['Cipher'])) { |
| 242 | throw new \InvalidArgumentException('An encryption cipher must be' |
| 243 | . ' specified in @CipherOptions["Cipher"].'); |
| 244 | } |
| 245 | |
| 246 | $algorithmSuite = AlgorithmSuite::validateCommitmentPolicyOnEncrypt( |
| 247 | $cipherOptions, |
| 248 | $keyCommitmentPolicy |
| 249 | ); |
| 250 | |
| 251 | $envelope = new MetadataEnvelope(); |
| 252 | |
| 253 | return Promise\Create::promiseFor( |
| 254 | //= ../specification/s3-encryption/client.md#required-api-operations |
| 255 | //= type=implication |
| 256 | //# - PutObject MUST encrypt its input data before it is uploaded to S3. |
| 257 | $this->encrypt( |
| 258 | Psr7\Utils::streamFor($args['Body']), |
| 259 | $algorithmSuite, |
| 260 | $options, |
| 261 | $provider, |
| 262 | $envelope |
| 263 | ) |
| 264 | )->then( |
| 265 | function ($encryptedBodyStream) use ($args) { |
| 266 | $hash = new PhpHash('sha256'); |
| 267 | $hashingEncryptedBodyStream = new HashingStream( |
| 268 | $encryptedBodyStream, |
| 269 | $hash, |
| 270 | self::getContentShaDecorator($args) |
| 271 | ); |
| 272 | |
| 273 | return [$hashingEncryptedBodyStream, $args]; |
| 274 | } |
| 275 | )->then( |
| 276 | function ($putObjectContents) use ($strategy, $envelope) { |
| 277 | list($bodyStream, $args) = $putObjectContents; |
| 278 | if ($strategy === null) { |
| 279 | $strategy = self::getDefaultStrategy(); |
| 280 | } |
| 281 | //= ../specification/s3-encryption/data-format/metadata-strategy.md#object-metadata |
| 282 | //# By default, the S3EC MUST store content metadata in the S3 Object Metadata. |
| 283 | $updatedArgs = $strategy->save($envelope, $args); |
| 284 | $updatedArgs['Body'] = $bodyStream; |
| 285 | |
| 286 | return $updatedArgs; |
| 287 | } |
| 288 | )->then( |
| 289 | function ($args) { |
| 290 | unset($args['@CipherOptions']); |
| 291 | |
| 292 | return $this->client->putObjectAsync($args); |
| 293 | } |
| 294 | ); |
| 295 | } |
| 296 | |
| 297 | private static function getContentShaDecorator(&$args): \Closure |
| 298 | { |
| 299 | return function ($hash) use (&$args) { |
| 300 | $args['ContentSHA256'] = bin2hex($hash); |
| 301 | }; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Encrypts the data in the 'Body' field of $args and uploads it to the |
| 306 | * specified location on S3. |
| 307 | * |
| 308 | * @param array $args Arguments for encrypting an object and uploading it |
| 309 | * to S3 via PutObject. |
| 310 | * |
| 311 | * The required configuration arguments are as follows: |
| 312 | * |
| 313 | * - @MaterialsProvider: (MaterialsProvider) Provides Cek, Iv, and Cek |
| 314 | * encrypting/decrypting for encryption metadata. |
| 315 | * - @CommitmentPolicy: (string) Must be set to 'FORBID_ENCRYPT_ALLOW_DECRYPT', |
| 316 | * 'REQUIRE_ENCRYPT_ALLOW_DECRYPT', or 'REQUIRE_ENCRYPT_REQUIRE_DECRYPT'. |
| 317 | * - 'FORBID_ENCRYPT_ALLOW_DECRYPT' indicates that the client is configured |
| 318 | * to write messages without key commitment and read messages encrypted |
| 319 | * with key commitment or without key commitment. |
| 320 | * - 'REQUIRE_ENCRYPT_ALLOW_DECRYPT' indicates that the client is configured |
| 321 | * to write messages with key commitment and read messages encrypted |
| 322 | * with key commitment or without key commitment. |
| 323 | * - 'REQUIRE_ENCRYPT_REQUIRE_DECRYPT' indicates that the client is configured |
| 324 | * to write messages with key commitment and read messages encrypted |
| 325 | * with key commitment. |
| 326 | * - @CipherOptions: (array) Cipher options for encrypting data. A Cipher |
| 327 | * is required. Accepts the following options: |
| 328 | * - Cipher: (string) gcm |
| 329 | * See also: AbstractCryptoClientV3::$supportedCiphers |
| 330 | * - KeySize: (int) 128|256 |
| 331 | * See also: MaterialsProvider::$supportedKeySizes |
| 332 | * - Aad: (string) Additional authentication data. This option is |
| 333 | * passed directly to OpenSSL when using gcm. Note if you pass in |
| 334 | * Aad, the PHP SDK will be able to decrypt the resulting object, |
| 335 | * but other AWS SDKs may not be able to do so. |
| 336 | * - @KmsEncryptionContext: (array) Only required if using |
| 337 | * KmsMaterialsProviderV3. An associative array of key-value |
| 338 | * pairs to be added to the encryption context for KMS key encryption. An |
| 339 | * empty array may be passed if no additional context is desired. |
| 340 | * |
| 341 | * The optional configuration arguments are as follows: |
| 342 | * |
| 343 | * - @MetadataStrategy: (MetadataStrategy|string|null) Strategy for storing |
| 344 | * MetadataEnvelope information. Defaults to using a |
| 345 | * HeadersMetadataStrategy. Can either be a class implementing |
| 346 | * MetadataStrategy, a class name of a predefined strategy, or empty/null |
| 347 | * to default. |
| 348 | * - @InstructionFileSuffix: (string|null) Suffix used when writing to an |
| 349 | * instruction file if an using an InstructionFileMetadataHandler was |
| 350 | * determined. |
| 351 | * |
| 352 | * @return \Aws\Result PutObject call result with the details of uploading |
| 353 | * the encrypted file. |
| 354 | * |
| 355 | * @throws \InvalidArgumentException Thrown when arguments above are not |
| 356 | * passed or are passed incorrectly. |
| 357 | */ |
| 358 | public function putObject(array $args): Result |
| 359 | { |
| 360 | //= ../specification/s3-encryption/client.md#required-api-operations |
| 361 | //= type=implication |
| 362 | //# - PutObject MUST be implemented by the S3EC. |
| 363 | return $this->putObjectAsync($args)->wait(); |
| 364 | } |
| 365 | |
| 366 | /** |
| 367 | * Promises to retrieve an object from S3 and decrypt the data in the |
| 368 | * 'Body' field. |
| 369 | * |
| 370 | * @param array $args Arguments for retrieving an object from S3 via |
| 371 | * GetObject and decrypting it. |
| 372 | * |
| 373 | * The required configuration argument is as follows: |
| 374 | * |
| 375 | * - @MaterialsProvider: (MaterialsProviderInterface) Provides Cek, Iv, and Cek |
| 376 | * encrypting/decrypting for decryption metadata. May have data loaded |
| 377 | * from the MetadataEnvelope upon decryption. |
| 378 | * - @CommitmentPolicy: (string) Must be set to 'FORBID_ENCRYPT_ALLOW_DECRYPT', |
| 379 | * 'REQUIRE_ENCRYPT_ALLOW_DECRYPT', or 'REQUIRE_ENCRYPT_REQUIRE_DECRYPT'. |
| 380 | * - 'FORBID_ENCRYPT_ALLOW_DECRYPT' indicates that the client is configured |
| 381 | * to write messages without key commitment and read messages encrypted |
| 382 | * with key commitment or without key commitment. |
| 383 | * - 'REQUIRE_ENCRYPT_ALLOW_DECRYPT' indicates that the client is configured |
| 384 | * to write messages with key commitment and read messages encrypted |
| 385 | * with key commitment or without key commitment. |
| 386 | * - 'REQUIRE_ENCRYPT_REQUIRE_DECRYPT' indicates that the client is configured |
| 387 | * to write messages with key commitment and read messages encrypted |
| 388 | * with key commitment. |
| 389 | * - @SecurityProfile: (string) Must be set to 'V3' or 'V3_AND_LEGACY'. |
| 390 | * - 'V3' indicates that only objects encrypted with S3EncryptionClientV3 |
| 391 | * content encryption and key wrap schemas are able to be decrypted. |
| 392 | * - 'V3_AND_LEGACY' indicates that objects encrypted with both |
| 393 | * S3EncryptionClientV3 and older legacy encryption clients are able |
| 394 | * to be decrypted. |
| 395 | * |
| 396 | * The optional configuration arguments are as follows: |
| 397 | * |
| 398 | * - SaveAs: (string) The path to a file on disk to save the decrypted |
| 399 | * object data. This will be handled by file_put_contents instead of the |
| 400 | * Guzzle sink. |
| 401 | * |
| 402 | * - @MetadataStrategy: (MetadataStrategy|string|null) Strategy for reading |
| 403 | * MetadataEnvelope information. Defaults to determining based on object |
| 404 | * response headers. Can either be a class implementing MetadataStrategy, |
| 405 | * a class name of a predefined strategy, or empty/null to default. |
| 406 | * - @InstructionFileSuffix: (string) Suffix used when looking for an |
| 407 | * instruction file if an InstructionFileMetadataHandler is being used. |
| 408 | * - @CipherOptions: (array) Cipher options for decrypting data. A Cipher |
| 409 | * is required. Accepts the following options: |
| 410 | * - Aad: (string) Additional authentication data. This option is |
| 411 | * passed directly to OpenSSL when using gcm. It is ignored when |
| 412 | * using cbc. |
| 413 | * - @KmsAllowDecryptWithAnyCmk: (bool) This allows decryption with |
| 414 | * KMS materials for any KMS key ID, instead of needing the KMS key ID to |
| 415 | * be specified and provided to the decrypt operation. Ignored for non-KMS |
| 416 | * materials providers. Defaults to false. |
| 417 | * |
| 418 | * @return PromiseInterface |
| 419 | * |
| 420 | * @throws \InvalidArgumentException Thrown when required arguments are not |
| 421 | * passed or are passed incorrectly. |
| 422 | */ |
| 423 | public function getObjectAsync(array $args): PromiseInterface |
| 424 | { |
| 425 | //= ../specification/s3-encryption/client.md#cryptographic-materials |
| 426 | //= type=implication |
| 427 | //# The S3EC MUST accept either one CMM or one Keyring instance upon initialization. |
| 428 | $provider = $this->getMaterialsProvider($args); |
| 429 | unset($args['@MaterialsProvider']); |
| 430 | |
| 431 | //= ../specification/s3-encryption/client.md#key-commitment |
| 432 | //= type=implication |
| 433 | //# The S3EC MUST support configuration of the [Key Commitment policy](./key-commitment.md) during its initialization. |
| 434 | $keyCommitmentPolicy = $this->getKeyCommitmentPolicy($args); |
| 435 | unset($args['@CommitmentPolicy']); |
| 436 | |
| 437 | $instructionFileSuffix = $this->getInstructionFileSuffix($args); |
| 438 | unset($args['@InstructionFileSuffix']); |
| 439 | |
| 440 | $strategy = $this->getMetadataStrategy($args, $instructionFileSuffix); |
| 441 | unset($args['@MetadataStrategy']); |
| 442 | |
| 443 | //= ../specification/s3-encryption/client.md#enable-legacy-wrapping-algorithms |
| 444 | //= type=implication |
| 445 | //# The S3EC MUST support the option to enable or disable legacy wrapping algorithms. |
| 446 | if (!isset($args['@SecurityProfile']) |
| 447 | || !in_array($args['@SecurityProfile'], self::SUPPORTED_SECURITY_PROFILES) |
| 448 | ) { |
| 449 | throw new CryptoException("@SecurityProfile is required and must be" |
| 450 | . " set to 'V3' or 'V3_AND_LEGACY'"); |
| 451 | } |
| 452 | |
| 453 | // Only throw this legacy warning once per client |
| 454 | if (in_array($args['@SecurityProfile'], self::LEGACY_SECURITY_PROFILES) |
| 455 | && $this->legacyWarningCount < 1 |
| 456 | ) { |
| 457 | $this->legacyWarningCount++; |
| 458 | trigger_error( |
| 459 | "This S3 Encryption Client operation is configured to" |
| 460 | . " read encrypted data with legacy encryption modes. If you" |
| 461 | . " don't have objects encrypted with these legacy modes," |
| 462 | . " you should disable support for them to enhance security. ", |
| 463 | E_USER_WARNING |
| 464 | ); |
| 465 | } |
| 466 | |
| 467 | $saveAs = null; |
| 468 | if (!empty($args['SaveAs'])) { |
| 469 | $saveAs = $args['SaveAs']; |
| 470 | } |
| 471 | |
| 472 | $promise = $this->client->getObjectAsync($args) |
| 473 | ->then( |
| 474 | function ($result) use ( |
| 475 | $provider, |
| 476 | $instructionFileSuffix, |
| 477 | $strategy, |
| 478 | $keyCommitmentPolicy, |
| 479 | $args |
| 480 | ) { |
| 481 | if ($strategy === null) { |
| 482 | $strategy = $this->determineGetObjectStrategy( |
| 483 | $result, |
| 484 | $instructionFileSuffix |
| 485 | ); |
| 486 | } |
| 487 | |
| 488 | $envelope = $strategy->load($args + [ |
| 489 | 'Metadata' => $result['Metadata'] |
| 490 | ]); |
| 491 | //= ../specification/s3-encryption/client.md#required-api-operations |
| 492 | //= type=implication |
| 493 | //# - GetObject MUST decrypt data received from the S3 server and return it as plaintext. |
| 494 | $result['Body'] = $this->decrypt( |
| 495 | $result['Body'], |
| 496 | $provider, |
| 497 | $envelope, |
| 498 | $keyCommitmentPolicy, |
| 499 | $args |
| 500 | ); |
| 501 | |
| 502 | return $result; |
| 503 | } |
| 504 | )->then( |
| 505 | function ($result) use ($saveAs) { |
| 506 | if (!empty($saveAs)) { |
| 507 | file_put_contents( |
| 508 | $saveAs, |
| 509 | (string)$result['Body'], |
| 510 | LOCK_EX |
| 511 | ); |
| 512 | } |
| 513 | |
| 514 | return $result; |
| 515 | } |
| 516 | ); |
| 517 | |
| 518 | return $promise; |
| 519 | } |
| 520 | |
| 521 | /** |
| 522 | * Retrieves an object from S3 and decrypts the data in the 'Body' field. |
| 523 | * |
| 524 | * @param array $args Arguments for retrieving an object from S3 via |
| 525 | * GetObject and decrypting it. |
| 526 | * |
| 527 | * The required configuration argument is as follows: |
| 528 | * |
| 529 | * - @MaterialsProvider: (MaterialsProviderInterface) Provides Cek, Iv, and Cek |
| 530 | * encrypting/decrypting for decryption metadata. May have data loaded |
| 531 | * from the MetadataEnvelope upon decryption. |
| 532 | * - @CommitmentPolicy: (string) Must be set to 'FORBID_ENCRYPT_ALLOW_DECRYPT', |
| 533 | * 'REQUIRE_ENCRYPT_ALLOW_DECRYPT', or 'REQUIRE_ENCRYPT_REQUIRE_DECRYPT'. |
| 534 | * - 'FORBID_ENCRYPT_ALLOW_DECRYPT' indicates that the client is configured |
| 535 | * to write messages without key commitment and read messages encrypted |
| 536 | * with key commitment or without key commitment. |
| 537 | * - 'REQUIRE_ENCRYPT_ALLOW_DECRYPT' indicates that the client is configured |
| 538 | * to write messages with key commitment and read messages encrypted |
| 539 | * with key commitment or without key commitment. |
| 540 | * - 'REQUIRE_ENCRYPT_REQUIRE_DECRYPT' indicates that the client is configured |
| 541 | * to write messages with key commitment and read messages encrypted |
| 542 | * with key commitment. |
| 543 | * - @SecurityProfile: (string) Must be set to 'V3' or 'V3_AND_LEGACY'. |
| 544 | * - 'V3' indicates that only objects encrypted with S3EncryptionClientV3 |
| 545 | * content encryption and key wrap schemas are able to be decrypted. |
| 546 | * - 'V3_AND_LEGACY' indicates that objects encrypted with both |
| 547 | * S3EncryptionClientV3 and older legacy encryption clients are able |
| 548 | * to be decrypted. |
| 549 | * |
| 550 | * The optional configuration arguments are as follows: |
| 551 | * |
| 552 | * - SaveAs: (string) The path to a file on disk to save the decrypted |
| 553 | * object data. This will be handled by file_put_contents instead of the |
| 554 | * Guzzle sink. |
| 555 | * - @InstructionFileSuffix: (string|null) Suffix used when looking for an |
| 556 | * instruction file if an InstructionFileMetadataHandler was detected. |
| 557 | * - @CipherOptions: (array) Cipher options for encrypting data. A Cipher |
| 558 | * is required. Accepts the following options: |
| 559 | * - Aad: (string) Additional authentication data. This option is |
| 560 | * passed directly to OpenSSL when using gcm. It is ignored when |
| 561 | * using cbc. |
| 562 | * - @KmsAllowDecryptWithAnyCmk: (bool) This allows decryption with |
| 563 | * KMS materials for any KMS key ID, instead of needing the KMS key ID to |
| 564 | * be specified and provided to the decrypt operation. Ignored for non-KMS |
| 565 | * materials providers. Defaults to false. |
| 566 | * |
| 567 | * @return \Aws\Result GetObject call result with the 'Body' field |
| 568 | * wrapped in a decryption stream with its metadata |
| 569 | * information. |
| 570 | * |
| 571 | * @throws \InvalidArgumentException Thrown when arguments above are not |
| 572 | * passed or are passed incorrectly. |
| 573 | */ |
| 574 | public function getObject(array $args): Result |
| 575 | { |
| 576 | //= ../specification/s3-encryption/client.md#required-api-operations |
| 577 | //= type=implication |
| 578 | //# - GetObject MUST be implemented by the S3EC. |
| 579 | return $this->getObjectAsync($args)->wait(); |
| 580 | } |
| 581 | } |