| 1 |
<?php |
| 2 |
namespace Aws\Crypto; |
| 3 |
|
| 4 |
use Aws\Exception\CryptoException; |
| 5 |
use GuzzleHttp\Psr7; |
| 6 |
use GuzzleHttp\Psr7\LimitStream; |
| 7 |
use Psr\Http\Message\StreamInterface; |
| 8 |
|
| 9 |
trait DecryptionTraitV2 |
| 10 |
{ |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
abstract protected function getCipherFromAesName($aesName); |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
abstract protected function buildCipherMethod($cipherName, $iv, $keySize); |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public function decrypt( |
| 59 |
$cipherText, |
| 60 |
MaterialsProviderInterfaceV2 $provider, |
| 61 |
MetadataEnvelope $envelope, |
| 62 |
array $options = [] |
| 63 |
) |
| 64 |
{ |
| 65 |
$commitmentPolicy = $this->getKeyCommitmentPolicy($options); |
| 66 |
unset($options['@CommitmentPolicy']); |
| 67 |
if (isset($envelope[MetadataEnvelope::ENCRYPTED_DATA_KEY_V3])) { |
| 68 |
if ($commitmentPolicy !== "FORBID_ENCRYPT_ALLOW_DECRYPT") { |
| 69 |
throw new CryptoException( |
| 70 |
"The requested item is encrypted" |
| 71 |
. " with Key Commitment. The current policy {$commitmentPolicy}" |
| 72 |
. " conflicts with the object metadata. Select an appropriate" |
| 73 |
. " '@CommitmentPolicy' to decrypt this item." |
| 74 |
); |
| 75 |
} |
| 76 |
$algorithmSuite = AlgorithmSuite::ALG_AES_256_GCM_HKDF_SHA512_COMMIT_KEY; |
| 77 |
$options['@CipherOptions'] = $options['@CipherOptions'] ?? []; |
| 78 |
$options['@CipherOptions']['Iv'] = str_repeat("\1", 12); |
| 79 |
$options['@CipherOptions']['TagLength'] = $algorithmSuite->getCipherTagLengthInBytes(); |
| 80 |
$materialDescription = json_decode( |
| 81 |
$envelope[MetadataEnvelope::ENCRYPTION_CONTEXT_V3], |
| 82 |
true |
| 83 |
); |
| 84 |
|
| 85 |
|
| 86 |
$cek = $provider->decryptCek( |
| 87 |
base64_decode($envelope[MetadataEnvelope::ENCRYPTED_DATA_KEY_V3]), |
| 88 |
$materialDescription, |
| 89 |
$options |
| 90 |
); |
| 91 |
|
| 92 |
$options['@CipherOptions']['KeySize'] = strlen($cek) * 8; |
| 93 |
$options['@CipherOptions']['Cipher'] = $this->getCipherFromAesName( |
| 94 |
$this->numericalContenCipherToAesName($envelope) |
| 95 |
); |
| 96 |
$this->validateOptionsAndEnvelope($options, $envelope); |
| 97 |
|
| 98 |
$messageId = base64_decode($envelope[MetadataEnvelope::MESSAGE_ID_V3]); |
| 99 |
$commitmentKey = base64_decode($envelope[MetadataEnvelope::KEY_COMMITMENT_V3]); |
| 100 |
|
| 101 |
if (strlen($messageId) !== ($algorithmSuite->getKeyCommitmentSaltLengthBits()) / 8) { |
| 102 |
throw new CryptoException("Invalid MessageId length found in object envelope."); |
| 103 |
} |
| 104 |
|
| 105 |
if (strlen($commitmentKey) !== $algorithmSuite->getCommitmentOutputKeyLengthBytes()) { |
| 106 |
throw new CryptoException("Invalid Commitment Key length found in object envelope."); |
| 107 |
} |
| 108 |
|
| 109 |
$decryptionStream = $this->getCommitingDecryptingStream( |
| 110 |
$cipherText, |
| 111 |
$cek, |
| 112 |
$options['@CipherOptions'], |
| 113 |
$messageId, |
| 114 |
$commitmentKey, |
| 115 |
$algorithmSuite |
| 116 |
); |
| 117 |
unset($cek); |
| 118 |
|
| 119 |
return $decryptionStream; |
| 120 |
} else { |
| 121 |
$options['@CipherOptions'] = $options['@CipherOptions'] ?? []; |
| 122 |
$options['@CipherOptions']['Iv'] = base64_decode( |
| 123 |
$envelope[MetadataEnvelope::IV_HEADER] |
| 124 |
); |
| 125 |
|
| 126 |
$options['@CipherOptions']['TagLength'] = |
| 127 |
$envelope[MetadataEnvelope::CRYPTO_TAG_LENGTH_HEADER] / 8; |
| 128 |
|
| 129 |
$cek = $provider->decryptCek( |
| 130 |
base64_decode( |
| 131 |
$envelope[MetadataEnvelope::CONTENT_KEY_V2_HEADER] |
| 132 |
), |
| 133 |
json_decode( |
| 134 |
$envelope[MetadataEnvelope::MATERIALS_DESCRIPTION_HEADER], |
| 135 |
true |
| 136 |
), |
| 137 |
$options |
| 138 |
); |
| 139 |
$options['@CipherOptions']['KeySize'] = strlen($cek) * 8; |
| 140 |
$options['@CipherOptions']['Cipher'] = $this->getCipherFromAesName( |
| 141 |
$envelope[MetadataEnvelope::CONTENT_CRYPTO_SCHEME_HEADER] |
| 142 |
); |
| 143 |
|
| 144 |
$this->validateOptionsAndEnvelope($options, $envelope); |
| 145 |
|
| 146 |
$decryptionStream = $this->getDecryptingStream( |
| 147 |
$cipherText, |
| 148 |
$cek, |
| 149 |
$options['@CipherOptions'] |
| 150 |
); |
| 151 |
unset($cek); |
| 152 |
|
| 153 |
return $decryptionStream; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
private function buildMaterialDescription( |
| 158 |
MetadataEnvelope $envelope |
| 159 |
): array |
| 160 |
{ |
| 161 |
switch ($envelope[MetadataEnvelope::ENCRYPTED_DATA_KEY_ALGORITHM_V3]) { |
| 162 |
case 12: |
| 163 |
return ['aws:x-amz-cek-alg' => '115']; |
| 164 |
default: |
| 165 |
throw new CryptoException( |
| 166 |
"Unknown Encrypted Data Key " |
| 167 |
. "wrapping algorithm found: " |
| 168 |
. "{$envelope[MetadataEnvelope::ENCRYPTED_DATA_KEY_ALGORITHM_V3]}" |
| 169 |
); |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
private function numericalContenCipherToAesName( |
| 174 |
MetadataEnvelope $envelope |
| 175 |
): string |
| 176 |
{ |
| 177 |
switch ($envelope[MetadataEnvelope::CONTENT_CIPHER_V3]) { |
| 178 |
case 115: |
| 179 |
return 'AES/GCM/NoPadding'; |
| 180 |
default: |
| 181 |
throw new CryptoException( |
| 182 |
"Unknown Encrypted Data Key " |
| 183 |
. "wrapping algorithm found: " |
| 184 |
. "{$envelope[MetadataEnvelope::ENCRYPTED_DATA_KEY_ALGORITHM_V3]}" |
| 185 |
); |
| 186 |
} |
| 187 |
|
| 188 |
} |
| 189 |
|
| 190 |
private function getTagFromCiphertextStream( |
| 191 |
StreamInterface $cipherText, |
| 192 |
$tagLength |
| 193 |
) |
| 194 |
{ |
| 195 |
$cipherTextSize = $cipherText->getSize(); |
| 196 |
if ($cipherTextSize == null || $cipherTextSize <= 0) { |
| 197 |
throw new \RuntimeException('Cannot decrypt a stream of unknown' |
| 198 |
. ' size.'); |
| 199 |
} |
| 200 |
return (string) new LimitStream( |
| 201 |
$cipherText, |
| 202 |
$tagLength, |
| 203 |
$cipherTextSize - $tagLength |
| 204 |
); |
| 205 |
} |
| 206 |
|
| 207 |
private function getStrippedCiphertextStream( |
| 208 |
StreamInterface $cipherText, |
| 209 |
$tagLength |
| 210 |
) |
| 211 |
{ |
| 212 |
$cipherTextSize = $cipherText->getSize(); |
| 213 |
if ($cipherTextSize == null || $cipherTextSize <= 0) { |
| 214 |
throw new \RuntimeException('Cannot decrypt a stream of unknown' |
| 215 |
. ' size.'); |
| 216 |
} |
| 217 |
return new LimitStream( |
| 218 |
$cipherText, |
| 219 |
$cipherTextSize - $tagLength, |
| 220 |
0 |
| 221 |
); |
| 222 |
} |
| 223 |
|
| 224 |
private function validateOptionsAndEnvelope($options, $envelope): void |
| 225 |
{ |
| 226 |
$allowedCiphers = AbstractCryptoClientV2::$supportedCiphers; |
| 227 |
$allowedKeywraps = AbstractCryptoClientV2::$supportedKeyWraps; |
| 228 |
if ($options['@SecurityProfile'] == 'V2_AND_LEGACY') { |
| 229 |
$allowedCiphers = array_unique(array_merge( |
| 230 |
$allowedCiphers, |
| 231 |
AbstractCryptoClient::$supportedCiphers |
| 232 |
)); |
| 233 |
$allowedKeywraps = array_unique(array_merge( |
| 234 |
$allowedKeywraps, |
| 235 |
AbstractCryptoClient::$supportedKeyWraps |
| 236 |
)); |
| 237 |
} |
| 238 |
|
| 239 |
$v1SchemaException = new CryptoException("The requested object is encrypted" |
| 240 |
. " with V1 encryption schemas that have been disabled by" |
| 241 |
. " client configuration @SecurityProfile=V2. Retry with" |
| 242 |
. " V2_AND_LEGACY enabled or reencrypt the object."); |
| 243 |
|
| 244 |
if (!in_array($options['@CipherOptions']['Cipher'], $allowedCiphers)) { |
| 245 |
if (in_array($options['@CipherOptions']['Cipher'], AbstractCryptoClient::$supportedCiphers)) { |
| 246 |
throw $v1SchemaException; |
| 247 |
} |
| 248 |
throw new CryptoException("The requested object is encrypted with" |
| 249 |
. " the cipher '{$options['@CipherOptions']['Cipher']}', which is not" |
| 250 |
. " supported for decryption with the selected security profile." |
| 251 |
. " This profile allows decryption with: " |
| 252 |
. implode(", ", $allowedCiphers)); |
| 253 |
} |
| 254 |
|
| 255 |
if (isset($envelope[MetadataEnvelope::ENCRYPTED_DATA_KEY_V3])) { |
| 256 |
if ($envelope[MetadataEnvelope::ENCRYPTED_DATA_KEY_ALGORITHM_V3] !== '12') { |
| 257 |
throw new CryptoException("The requested object is encrypted with" |
| 258 |
. " the keywrap schema '{$envelope[MetadataEnvelope::ENCRYPTED_DATA_KEY_ALGORITHM_V3]}'," |
| 259 |
. " which is not supported for decryption with the current security" |
| 260 |
. " profile."); |
| 261 |
} |
| 262 |
} else { |
| 263 |
if (!in_array($envelope[MetadataEnvelope::KEY_WRAP_ALGORITHM_HEADER], $allowedKeywraps)) { |
| 264 |
if (in_array($envelope[MetadataEnvelope::KEY_WRAP_ALGORITHM_HEADER], AbstractCryptoClient::$supportedKeyWraps)) { |
| 265 |
throw $v1SchemaException; |
| 266 |
} |
| 267 |
|
| 268 |
throw new CryptoException("The requested object is encrypted with" |
| 269 |
. " the keywrap schema '{$envelope[MetadataEnvelope::KEY_WRAP_ALGORITHM_HEADER]}'," |
| 270 |
. " which is not supported for decryption with the current security" |
| 271 |
. " profile."); |
| 272 |
} |
| 273 |
$matdesc = json_decode( |
| 274 |
$envelope[MetadataEnvelope::MATERIALS_DESCRIPTION_HEADER], |
| 275 |
true |
| 276 |
); |
| 277 |
if (isset($matdesc['aws:x-amz-cek-alg']) |
| 278 |
&& ($envelope[MetadataEnvelope::CONTENT_CRYPTO_SCHEME_HEADER] |
| 279 |
!== $matdesc['aws:x-amz-cek-alg']) |
| 280 |
) { |
| 281 |
throw new CryptoException("There is a mismatch in specified content" |
| 282 |
. " encryption algrithm between the materials description value" |
| 283 |
. " and the metadata envelope value: {$matdesc['aws:x-amz-cek-alg']}" |
| 284 |
. " vs. {$envelope[MetadataEnvelope::CONTENT_CRYPTO_SCHEME_HEADER]}."); |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
protected function getDecryptingStream( |
| 305 |
$cipherText, |
| 306 |
$cek, |
| 307 |
$cipherOptions |
| 308 |
) |
| 309 |
{ |
| 310 |
$cipherTextStream = Psr7\Utils::streamFor($cipherText); |
| 311 |
switch ($cipherOptions['Cipher']) { |
| 312 |
case 'gcm': |
| 313 |
$cipherOptions['Tag'] = $this->getTagFromCiphertextStream( |
| 314 |
$cipherTextStream, |
| 315 |
$cipherOptions['TagLength'] |
| 316 |
); |
| 317 |
|
| 318 |
return new AesGcmDecryptingStream( |
| 319 |
$this->getStrippedCiphertextStream( |
| 320 |
$cipherTextStream, |
| 321 |
$cipherOptions['TagLength'] |
| 322 |
), |
| 323 |
$cek, |
| 324 |
$cipherOptions['Iv'], |
| 325 |
$cipherOptions['Tag'], |
| 326 |
$cipherOptions['Aad'] = $cipherOptions['Aad'] ?? '', |
| 327 |
$cipherOptions['TagLength'] ?: null, |
| 328 |
$cipherOptions['KeySize'] |
| 329 |
); |
| 330 |
default: |
| 331 |
$cipherMethod = $this->buildCipherMethod( |
| 332 |
$cipherOptions['Cipher'], |
| 333 |
$cipherOptions['Iv'], |
| 334 |
$cipherOptions['KeySize'] |
| 335 |
); |
| 336 |
return new AesDecryptingStream( |
| 337 |
$cipherTextStream, |
| 338 |
$cek, |
| 339 |
$cipherMethod |
| 340 |
); |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
protected function getCommitingDecryptingStream( |
| 365 |
string $cipherText, |
| 366 |
string $cek, |
| 367 |
array $cipherOptions, |
| 368 |
string $messageId, |
| 369 |
string $commitmentKey, |
| 370 |
AlgorithmSuite $algorithmSuite |
| 371 |
): AesStreamInterface|CryptoException |
| 372 |
{ |
| 373 |
$algorithmSuiteIdAsBytes = pack('n', $algorithmSuite->getId()); |
| 374 |
$derivedEncryptionKeyInfo = $algorithmSuiteIdAsBytes . "DERIVEKEY"; |
| 375 |
$commitmentKeyInfo = $algorithmSuiteIdAsBytes . "COMMITKEY"; |
| 376 |
$derivedEncryptionKey = hash_hkdf( |
| 377 |
$algorithmSuite->getHashingAlgorithm(), |
| 378 |
$cek, |
| 379 |
$algorithmSuite->getDerivationOutputKeyLengthBytes(), |
| 380 |
$derivedEncryptionKeyInfo, |
| 381 |
$messageId |
| 382 |
); |
| 383 |
$calculatedCommitmentKey = hash_hkdf( |
| 384 |
$algorithmSuite->getHashingAlgorithm(), |
| 385 |
$cek, |
| 386 |
$algorithmSuite->getCommitmentOutputKeyLengthBytes(), |
| 387 |
$commitmentKeyInfo, |
| 388 |
$messageId |
| 389 |
); |
| 390 |
|
| 391 |
if ($commitmentKey != $calculatedCommitmentKey) { |
| 392 |
throw new CryptoException("Calculated commitment key does " |
| 393 |
. "not match expected commitment key value "); |
| 394 |
} |
| 395 |
|
| 396 |
$cipherTextStream = Psr7\Utils::streamFor($cipherText); |
| 397 |
switch ($cipherOptions['Cipher']) { |
| 398 |
case 'gcm': |
| 399 |
$cipherOptions['Tag'] = $this->getTagFromCiphertextStream( |
| 400 |
$cipherTextStream, |
| 401 |
$cipherOptions['TagLength'] |
| 402 |
); |
| 403 |
$cipherOptions['Aad'] = isset($cipherOptions['Aad']) |
| 404 |
? $cipherOptions['Aad'] + $algorithmSuiteIdAsBytes |
| 405 |
: $algorithmSuiteIdAsBytes; |
| 406 |
|
| 407 |
return new AesGcmDecryptingStream( |
| 408 |
$this->getStrippedCiphertextStream( |
| 409 |
$cipherTextStream, |
| 410 |
$cipherOptions['TagLength'] |
| 411 |
), |
| 412 |
$derivedEncryptionKey, |
| 413 |
$cipherOptions['Iv'], |
| 414 |
$cipherOptions['Tag'], |
| 415 |
$cipherOptions['Aad'], |
| 416 |
$cipherOptions['TagLength'] ?: null, |
| 417 |
$cipherOptions['KeySize'] |
| 418 |
); |
| 419 |
default: |
| 420 |
throw new CryptoException("Unsupported Cipher used for key commitment messages." |
| 421 |
. " Found {$cipherOptions["Cipher"]}. Only 'gcm' is supported."); |
| 422 |
} |
| 423 |
} |
| 424 |
} |