| 1 |
<?php |
| 2 |
/** |
| 3 |
* Copyright 2016 Google Inc. All Rights Reserved. |
| 4 |
* |
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 |
* you may not use this file except in compliance with the License. |
| 7 |
* You may obtain a copy of the License at |
| 8 |
* |
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 10 |
* |
| 11 |
* Unless required by applicable law or agreed to in writing, software |
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
|
| 18 |
namespace Google\Cloud\Storage; |
| 19 |
|
| 20 |
use phpseclib\Crypt\RSA; |
| 21 |
|
| 22 |
/** |
| 23 |
* Trait which provides helper methods for customer-supplied encryption. |
| 24 |
*/ |
| 25 |
trait EncryptionTrait |
| 26 |
{ |
| 27 |
/** |
| 28 |
* @var array |
| 29 |
*/ |
| 30 |
private $copySourceEncryptionHeaderNames = [ |
| 31 |
'algorithm' => 'x-goog-copy-source-encryption-algorithm', |
| 32 |
'key' => 'x-goog-copy-source-encryption-key', |
| 33 |
'keySHA256' => 'x-goog-copy-source-encryption-key-sha256' |
| 34 |
]; |
| 35 |
|
| 36 |
/** |
| 37 |
* @var array |
| 38 |
*/ |
| 39 |
private $encryptionHeaderNames = [ |
| 40 |
'algorithm' => 'x-goog-encryption-algorithm', |
| 41 |
'key' => 'x-goog-encryption-key', |
| 42 |
'keySHA256' => 'x-goog-encryption-key-sha256' |
| 43 |
]; |
| 44 |
|
| 45 |
/** |
| 46 |
* Formats options for customer-supplied encryption headers. |
| 47 |
* |
| 48 |
* @param array $options |
| 49 |
* @return array |
| 50 |
* @access private |
| 51 |
*/ |
| 52 |
public function formatEncryptionHeaders(array $options) |
| 53 |
{ |
| 54 |
$encryptionHeaders = []; |
| 55 |
$useCopySourceHeaders = isset($options['useCopySourceHeaders']) ? $options['useCopySourceHeaders'] : false; |
| 56 |
$key = isset($options['encryptionKey']) ? $options['encryptionKey'] : null; |
| 57 |
$keySHA256 = isset($options['encryptionKeySHA256']) ? $options['encryptionKeySHA256'] : null; |
| 58 |
$destinationKey = isset($options['destinationEncryptionKey']) ? $options['destinationEncryptionKey'] : null; |
| 59 |
$destinationKeySHA256 = isset($options['destinationEncryptionKeySHA256']) |
| 60 |
? $options['destinationEncryptionKeySHA256'] |
| 61 |
: null; |
| 62 |
|
| 63 |
unset($options['useCopySourceHeaders']); |
| 64 |
unset($options['encryptionKey']); |
| 65 |
unset($options['encryptionKeySHA256']); |
| 66 |
unset($options['destinationEncryptionKey']); |
| 67 |
unset($options['destinationEncryptionKeySHA256']); |
| 68 |
|
| 69 |
$encryptionHeaders = $this->buildHeaders($key, $keySHA256, $useCopySourceHeaders) |
| 70 |
+ $this->buildHeaders($destinationKey, $destinationKeySHA256, false); |
| 71 |
|
| 72 |
if (!empty($encryptionHeaders)) { |
| 73 |
if (isset($options['restOptions']['headers'])) { |
| 74 |
$options['restOptions']['headers'] += $encryptionHeaders; |
| 75 |
} else { |
| 76 |
$options['restOptions']['headers'] = $encryptionHeaders; |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
return $options; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Builds out customer-supplied encryption headers. |
| 85 |
* |
| 86 |
* @param string $key |
| 87 |
* @param string $keySHA256 |
| 88 |
* @param bool $useCopySourceHeaders |
| 89 |
* @return array |
| 90 |
*/ |
| 91 |
private function buildHeaders($key, $keySHA256, $useCopySourceHeaders) |
| 92 |
{ |
| 93 |
if ($key) { |
| 94 |
$headerNames = $useCopySourceHeaders |
| 95 |
? $this->copySourceEncryptionHeaderNames |
| 96 |
: $this->encryptionHeaderNames; |
| 97 |
|
| 98 |
if (!$keySHA256) { |
| 99 |
$decodedKey = base64_decode($key); |
| 100 |
$keySHA256 = base64_encode(hash('SHA256', $decodedKey, true)); |
| 101 |
} |
| 102 |
|
| 103 |
return [ |
| 104 |
$headerNames['algorithm'] => 'AES256', |
| 105 |
$headerNames['key'] => $key, |
| 106 |
$headerNames['keySHA256'] => $keySHA256 |
| 107 |
]; |
| 108 |
} |
| 109 |
|
| 110 |
return []; |
| 111 |
} |
| 112 |
|
| 113 |
/** |
| 114 |
* Sign a string using a given private key. |
| 115 |
* |
| 116 |
* @deprecated Please use the {@see Google\Auth\SignBlobInterface::signBlob()} |
| 117 |
* and implementations for signing strings. |
| 118 |
* This method will be removed in a future release. |
| 119 |
* |
| 120 |
* @param string $privateKey The private key to use to sign the data. |
| 121 |
* @param string $data The data to sign. |
| 122 |
* @param bool $forceOpenssl If true, OpenSSL will be used regardless of |
| 123 |
* whether phpseclib is available. **Defaults to** `false`. |
| 124 |
* @return string The signature |
| 125 |
*/ |
| 126 |
protected function signString($privateKey, $data, $forceOpenssl = false) |
| 127 |
{ |
| 128 |
$signature = ''; |
| 129 |
|
| 130 |
if (class_exists(RSA::class) && !$forceOpenssl) { |
| 131 |
$rsa = new RSA; |
| 132 |
$rsa->loadKey($privateKey); |
| 133 |
$rsa->setSignatureMode(RSA::SIGNATURE_PKCS1); |
| 134 |
$rsa->setHash('sha256'); |
| 135 |
|
| 136 |
$signature = $rsa->sign($data); |
| 137 |
} elseif (extension_loaded('openssl')) { |
| 138 |
openssl_sign($data, $signature, $privateKey, 'sha256WithRSAEncryption'); |
| 139 |
} else { |
| 140 |
// @codeCoverageIgnoreStart |
| 141 |
throw new \RuntimeException('OpenSSL is not installed.'); |
| 142 |
} |
| 143 |
// @codeCoverageIgnoreEnd |
| 144 |
|
| 145 |
return $signature; |
| 146 |
} |
| 147 |
} |
| 148 |
|