Crypto
11 months ago
Exception
11 months ago
RegionalEndpoint
11 months ago
UseArnRegion
11 months ago
AmbiguousSuccessParser.php
11 months ago
ApplyChecksumMiddleware.php
11 months ago
BatchDelete.php
11 months ago
BucketEndpointArnMiddleware.php
11 months ago
BucketEndpointMiddleware.php
11 months ago
CalculatesChecksumTrait.php
11 months ago
EndpointRegionHelperTrait.php
11 months ago
GetBucketLocationParser.php
11 months ago
MultipartCopy.php
11 months ago
MultipartUploader.php
11 months ago
MultipartUploadingTrait.php
11 months ago
ObjectCopier.php
11 months ago
ObjectUploader.php
11 months ago
PermanentRedirectMiddleware.php
11 months ago
PostObject.php
11 months ago
PostObjectV4.php
11 months ago
PutObjectUrlMiddleware.php
11 months ago
RetryableMalformedResponseParser.php
11 months ago
S3Client.php
11 months ago
S3ClientInterface.php
11 months ago
S3ClientTrait.php
11 months ago
S3EndpointMiddleware.php
11 months ago
S3MultiRegionClient.php
11 months ago
S3UriParser.php
11 months ago
SSECMiddleware.php
11 months ago
StreamWrapper.php
11 months ago
Transfer.php
11 months ago
ValidateResponseChecksumParser.php
11 months ago
SSECMiddleware.php
76 lines
| 1 | <?php |
| 2 | namespace Aws\S3; |
| 3 | |
| 4 | use Aws\CommandInterface; |
| 5 | use Psr\Http\Message\RequestInterface; |
| 6 | |
| 7 | /** |
| 8 | * Simplifies the SSE-C process by encoding and hashing the key. |
| 9 | * @internal |
| 10 | */ |
| 11 | class SSECMiddleware |
| 12 | { |
| 13 | private $endpointScheme; |
| 14 | private $nextHandler; |
| 15 | |
| 16 | /** |
| 17 | * Provide the URI scheme of the client sending requests. |
| 18 | * |
| 19 | * @param string $endpointScheme URI scheme (http/https). |
| 20 | * |
| 21 | * @return callable |
| 22 | */ |
| 23 | public static function wrap($endpointScheme) |
| 24 | { |
| 25 | return function (callable $handler) use ($endpointScheme) { |
| 26 | return new self($endpointScheme, $handler); |
| 27 | }; |
| 28 | } |
| 29 | |
| 30 | public function __construct($endpointScheme, callable $nextHandler) |
| 31 | { |
| 32 | $this->nextHandler = $nextHandler; |
| 33 | $this->endpointScheme = $endpointScheme; |
| 34 | } |
| 35 | |
| 36 | public function __invoke( |
| 37 | CommandInterface $command, |
| 38 | RequestInterface $request = null |
| 39 | ) { |
| 40 | // Allows only HTTPS connections when using SSE-C |
| 41 | if (($command['SSECustomerKey'] || $command['CopySourceSSECustomerKey']) |
| 42 | && $this->endpointScheme !== 'https' |
| 43 | ) { |
| 44 | throw new \RuntimeException('You must configure your S3 client to ' |
| 45 | . 'use HTTPS in order to use the SSE-C features.'); |
| 46 | } |
| 47 | |
| 48 | // Prepare the normal SSE-CPK headers |
| 49 | if ($command['SSECustomerKey']) { |
| 50 | $this->prepareSseParams($command); |
| 51 | } |
| 52 | |
| 53 | // If it's a copy operation, prepare the SSE-CPK headers for the source. |
| 54 | if ($command['CopySourceSSECustomerKey']) { |
| 55 | $this->prepareSseParams($command, 'CopySource'); |
| 56 | } |
| 57 | |
| 58 | $f = $this->nextHandler; |
| 59 | return $f($command, $request); |
| 60 | } |
| 61 | |
| 62 | private function prepareSseParams(CommandInterface $command, $prefix = '') |
| 63 | { |
| 64 | // Base64 encode the provided key |
| 65 | $key = $command[$prefix . 'SSECustomerKey']; |
| 66 | $command[$prefix . 'SSECustomerKey'] = base64_encode($key); |
| 67 | |
| 68 | // Base64 the provided MD5 or, generate an MD5 if not provided |
| 69 | if ($md5 = $command[$prefix . 'SSECustomerKeyMD5']) { |
| 70 | $command[$prefix . 'SSECustomerKeyMD5'] = base64_encode($md5); |
| 71 | } else { |
| 72 | $command[$prefix . 'SSECustomerKeyMD5'] = base64_encode(md5($key, true)); |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 |