| 1 |
<?php |
| 2 |
namespace Aws\Crypto; |
| 3 |
|
| 4 |
use Aws\Crypto\Cipher\CipherMethod; |
| 5 |
use Aws\Exception\CryptoException; |
| 6 |
use GuzzleHttp\Psr7; |
| 7 |
use GuzzleHttp\Psr7\AppendStream; |
| 8 |
use GuzzleHttp\Psr7\Stream; |
| 9 |
use Psr\Http\Message\StreamInterface; |
| 10 |
|
| 11 |
trait EncryptionTraitV3 |
| 12 |
{ |
| 13 |
private static array $allowedOptions = [ |
| 14 |
'Cipher' => true, |
| 15 |
'KeySize' => true, |
| 16 |
'Aad' => true, |
| 17 |
]; |
| 18 |
|
| 19 |
private static array $encryptClasses = [ |
| 20 |
'gcm' => AesGcmEncryptingStream::class |
| 21 |
]; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
abstract protected function buildCipherMethod( |
| 37 |
$cipherName, |
| 38 |
$iv, |
| 39 |
$keySize |
| 40 |
); |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function encrypt( |
| 64 |
Stream $plaintext, |
| 65 |
AlgorithmSuite $algorithmSuite, |
| 66 |
array $options, |
| 67 |
MaterialsProviderV3 $provider, |
| 68 |
MetadataEnvelope $envelope |
| 69 |
): AppendStream |
| 70 |
{ |
| 71 |
$options = array_change_key_case($options); |
| 72 |
$cipherOptions = array_intersect_key( |
| 73 |
$options['@cipheroptions'], |
| 74 |
self::$allowedOptions |
| 75 |
); |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
if (strlen($plaintext) > $algorithmSuite->getCipherMaxContentLengthBytes()) { |
| 81 |
throw new \InvalidArgumentException("The contentLength of the object you are attempting" |
| 82 |
. " to encrypt exceeds the maximum length allowed for GCM encryption."); |
| 83 |
} |
| 84 |
$cipherOptions['Cipher'] = strtolower($cipherOptions['Cipher']); |
| 85 |
|
| 86 |
if (!self::isSupportedCipher($cipherOptions['Cipher'])) { |
| 87 |
throw new \InvalidArgumentException('The cipher requested is not' |
| 88 |
. ' supported by the SDK.'); |
| 89 |
} |
| 90 |
|
| 91 |
if (empty($cipherOptions['KeySize'])) { |
| 92 |
$cipherOptions['KeySize'] = 256; |
| 93 |
} |
| 94 |
|
| 95 |
if (!is_int($cipherOptions['KeySize'])) { |
| 96 |
throw new \InvalidArgumentException('The cipher "KeySize" must be' |
| 97 |
. ' an integer.'); |
| 98 |
} |
| 99 |
|
| 100 |
if (!MaterialsProviderV3::isSupportedKeySize($cipherOptions['KeySize'])) { |
| 101 |
throw new \InvalidArgumentException('The cipher "KeySize" requested' |
| 102 |
. ' is not supported by AES (256).'); |
| 103 |
} |
| 104 |
|
| 105 |
$encryptClass = self::$encryptClasses[$algorithmSuite->getCipherName()]; |
| 106 |
$aesName = $encryptClass::getStaticAesName(); |
| 107 |
$materialsDescription = $algorithmSuite->isKeyCommitting() |
| 108 |
? ['aws:x-amz-cek-alg' => '115'] |
| 109 |
: ['aws:x-amz-cek-alg' => $aesName]; |
| 110 |
|
| 111 |
$keys = $provider->generateCek( |
| 112 |
$algorithmSuite->getDataKeyLengthBits(), |
| 113 |
$materialsDescription, |
| 114 |
$options |
| 115 |
); |
| 116 |
|
| 117 |
|
| 118 |
if (isset($keys['UpdatedContext'])) { |
| 119 |
$materialsDescription = $keys['UpdatedContext']; |
| 120 |
} |
| 121 |
|
| 122 |
if ($algorithmSuite->isKeyCommitting()) { |
| 123 |
return $this->encryptCommitingStream( |
| 124 |
$plaintext, |
| 125 |
$algorithmSuite, |
| 126 |
$cipherOptions, |
| 127 |
$keys, |
| 128 |
$materialsDescription, |
| 129 |
$provider, |
| 130 |
$envelope |
| 131 |
); |
| 132 |
} else { |
| 133 |
return $this->encryptNonCommitingStream( |
| 134 |
$plaintext, |
| 135 |
$cipherOptions, |
| 136 |
$keys, |
| 137 |
$materialsDescription, |
| 138 |
$aesName, |
| 139 |
$provider, |
| 140 |
$envelope |
| 141 |
); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
private function encryptNonCommitingStream( |
| 146 |
Stream $plaintext, |
| 147 |
array &$cipherOptions, |
| 148 |
array $keys, |
| 149 |
array $materialsDescription, |
| 150 |
string $aesName, |
| 151 |
MaterialsProviderV3 $provider, |
| 152 |
MetadataEnvelope $envelope |
| 153 |
): AppendStream |
| 154 |
{ |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
$cipherOptions['Iv'] = $provider->generateIv( |
| 161 |
$this->getCipherOpenSslName( |
| 162 |
$cipherOptions['Cipher'], |
| 163 |
$cipherOptions['KeySize'] |
| 164 |
) |
| 165 |
); |
| 166 |
|
| 167 |
if (isset($keys['UpdatedContext'])) { |
| 168 |
$materialsDescription = $keys['UpdatedContext']; |
| 169 |
} |
| 170 |
|
| 171 |
$encryptingStream = $this->getNonCommittingEncryptingStream( |
| 172 |
$plaintext, |
| 173 |
$keys['Plaintext'], |
| 174 |
$cipherOptions |
| 175 |
); |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
$envelope[MetadataEnvelope::CONTENT_KEY_V2_HEADER] = $keys['Ciphertext']; |
| 181 |
unset($keys); |
| 182 |
|
| 183 |
$envelope[MetadataEnvelope::IV_HEADER] = |
| 184 |
base64_encode($cipherOptions['Iv']); |
| 185 |
$envelope[MetadataEnvelope::KEY_WRAP_ALGORITHM_HEADER] = |
| 186 |
$provider->getWrapAlgorithmName(); |
| 187 |
$envelope[MetadataEnvelope::CONTENT_CRYPTO_SCHEME_HEADER] = $aesName; |
| 188 |
$envelope[MetadataEnvelope::UNENCRYPTED_CONTENT_LENGTH_HEADER] = |
| 189 |
(string) strlen($plaintext); |
| 190 |
$envelope[MetadataEnvelope::MATERIALS_DESCRIPTION_HEADER] = |
| 191 |
json_encode($materialsDescription); |
| 192 |
if (!empty($cipherOptions['Tag'])) { |
| 193 |
$envelope[MetadataEnvelope::CRYPTO_TAG_LENGTH_HEADER] = |
| 194 |
(string) (strlen($cipherOptions['Tag']) * 8); |
| 195 |
} |
| 196 |
if (!MetadataEnvelope::isV2Envelope($envelope)) { |
| 197 |
throw new CryptoException("Error while writing metadata envelope." |
| 198 |
. " Not all required fields were set."); |
| 199 |
} |
| 200 |
|
| 201 |
return $encryptingStream; |
| 202 |
} |
| 203 |
|
| 204 |
private function encryptCommitingStream( |
| 205 |
Stream $plaintext, |
| 206 |
AlgorithmSuite $algorithmSuite, |
| 207 |
array &$options, |
| 208 |
array $keys, |
| 209 |
array $materialsDescription, |
| 210 |
MaterialsProviderV3 $provider, |
| 211 |
MetadataEnvelope $envelope |
| 212 |
): AppendStream |
| 213 |
{ |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
$options['Iv'] = str_repeat("\1", $algorithmSuite->getIvLengthBytes()); |
| 226 |
$messageId = $provider->generateIv( |
| 227 |
$this->getCipherOpenSslName( |
| 228 |
$algorithmSuite->getCipherName(), |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
$algorithmSuite->getKeyCommitmentSaltLengthBits() |
| 233 |
) |
| 234 |
); |
| 235 |
|
| 236 |
if (isset($keys['UpdatedContext'])) { |
| 237 |
$materialsDescription["aws:x-amz-cek-alg"] = (string) $algorithmSuite->getId(); |
| 238 |
} |
| 239 |
$commitingEncryptingArray = $this->getCommitingEncryptionStream( |
| 240 |
$plaintext, |
| 241 |
$keys['Plaintext'], |
| 242 |
$options, |
| 243 |
$messageId, |
| 244 |
$algorithmSuite |
| 245 |
); |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
$commitmentKey = $commitingEncryptingArray[0]; |
| 251 |
$encryptionStream = $commitingEncryptingArray[1]; |
| 252 |
|
| 253 |
|
| 254 |
$envelope[MetadataEnvelope::ENCRYPTED_DATA_KEY_V3] = $keys['Ciphertext']; |
| 255 |
unset($keys); |
| 256 |
|
| 257 |
$envelope[MetadataEnvelope::CONTENT_CIPHER_V3] = $algorithmSuite->getId(); |
| 258 |
|
| 259 |
|
| 260 |
$envelope[MetadataEnvelope::ENCRYPTION_CONTEXT_V3] = |
| 261 |
json_encode($materialsDescription); |
| 262 |
|
| 263 |
|
| 264 |
$envelope[MetadataEnvelope::ENCRYPTED_DATA_KEY_ALGORITHM_V3] = '12'; |
| 265 |
$envelope[MetadataEnvelope::KEY_COMMITMENT_V3] = $commitmentKey; |
| 266 |
$envelope[MetadataEnvelope::MESSAGE_ID_V3] = base64_encode($messageId); |
| 267 |
if (!MetadataEnvelope::isV3Envelope($envelope)) { |
| 268 |
throw new CryptoException("Error while writing metadata envelope." |
| 269 |
. " Not all required fields were set."); |
| 270 |
} |
| 271 |
|
| 272 |
return $encryptionStream; |
| 273 |
} |
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
protected function getNonCommittingEncryptingStream( |
| 291 |
Stream $plaintext, |
| 292 |
string $cek, |
| 293 |
array &$cipherOptions |
| 294 |
): AppendStream |
| 295 |
{ |
| 296 |
|
| 297 |
switch ($cipherOptions['Cipher']) { |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
case 'gcm': |
| 302 |
$cipherOptions['TagLength'] = 16; |
| 303 |
$encryptClass = self::$encryptClasses['gcm']; |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
$cipherTextStream = new $encryptClass( |
| 310 |
$plaintext, |
| 311 |
$cek, |
| 312 |
$cipherOptions['Iv'], |
| 313 |
$cipherOptions['Aad'] = isset($cipherOptions['Aad']) |
| 314 |
? $cipherOptions['Aad'] |
| 315 |
: '', |
| 316 |
$cipherOptions['TagLength'], |
| 317 |
$cipherOptions['KeySize'] |
| 318 |
); |
| 319 |
|
| 320 |
if (!empty($cipherOptions['Aad'])) { |
| 321 |
trigger_error("'Aad' has been supplied for content encryption" |
| 322 |
. " with " . $cipherTextStream->getAesName() . ". The" |
| 323 |
. " PHP SDK encryption client can decrypt an object" |
| 324 |
. " encrypted in this way, but other AWS SDKs may not be" |
| 325 |
. " able to.", E_USER_WARNING); |
| 326 |
} |
| 327 |
|
| 328 |
$appendStream = new AppendStream([ |
| 329 |
$cipherTextStream->createStream() |
| 330 |
]); |
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
$cipherOptions['Tag'] = $cipherTextStream->getTag(); |
| 336 |
$appendStream->addStream(Psr7\Utils::streamFor($cipherOptions['Tag'])); |
| 337 |
|
| 338 |
return $appendStream; |
| 339 |
default: |
| 340 |
throw new CryptoException("Unsupported Cipher used for key commitment messages." |
| 341 |
. " Found {$cipherOptions["Cipher"]}. Only 'gcm' is supported."); |
| 342 |
} |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
protected function getCommitingEncryptionStream( |
| 364 |
Stream $plaintext, |
| 365 |
string $dek, |
| 366 |
array &$cipherOptions, |
| 367 |
string $messageId, |
| 368 |
AlgorithmSuite $algorithmSuite |
| 369 |
): array |
| 370 |
{ |
| 371 |
$algorithmSuiteIdAsBytes = pack('n', $algorithmSuite->getId()); |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
$derivedEncryptionKeyInfo = $algorithmSuiteIdAsBytes . "DERIVEKEY"; |
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
$commitmentKeyInfo = $algorithmSuiteIdAsBytes . "COMMITKEY"; |
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
if (strlen($dek) !== $algorithmSuite->getDerivationInputKeyLengthBytes()) { |
| 385 |
throw new CryptoException("Input Key Material length exceeds " |
| 386 |
. "key derivation input length specified by the algorithm suite."); |
| 387 |
} |
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
$cek = hash_hkdf( |
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
$algorithmSuite->getHashingAlgorithm(), |
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
$dek, |
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
$algorithmSuite->getDerivationOutputKeyLengthBytes(), |
| 408 |
$derivedEncryptionKeyInfo, |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
$messageId |
| 413 |
); |
| 414 |
$commitmentKey = hash_hkdf( |
| 415 |
$algorithmSuite->getHashingAlgorithm(), |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
$dek, |
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
$algorithmSuite->getCommitmentOutputKeyLengthBytes(), |
| 424 |
$commitmentKeyInfo, |
| 425 |
$messageId |
| 426 |
); |
| 427 |
|
| 428 |
switch ($cipherOptions['Cipher']) { |
| 429 |
|
| 430 |
case 'gcm': |
| 431 |
$cipherOptions['TagLength'] = $algorithmSuite->getCipherTagLengthInBytes(); |
| 432 |
$encryptClass = self::$encryptClasses[$algorithmSuite->getCipherName()]; |
| 433 |
|
| 434 |
if (!empty($cipherOptions['Aad'])) { |
| 435 |
trigger_error("'Aad' has been supplied for content encryption" |
| 436 |
. " with " . $encryptClass->getAesName() . ". The" |
| 437 |
. " PHP SDK encryption client can decrypt an object" |
| 438 |
. " encrypted in this way, but other AWS SDKs may not be" |
| 439 |
. " able to.", E_USER_NOTICE); |
| 440 |
} |
| 441 |
$cipherOptions['Aad'] = isset($cipherOptions['Aad']) |
| 442 |
? $cipherOptions['Aad'] + $algorithmSuiteIdAsBytes |
| 443 |
: $algorithmSuiteIdAsBytes; |
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
$cipherTextStream = new $encryptClass( |
| 451 |
$plaintext, |
| 452 |
$cek, |
| 453 |
$cipherOptions['Iv'], |
| 454 |
$cipherOptions['Aad'], |
| 455 |
$cipherOptions['TagLength'], |
| 456 |
$cipherOptions['KeySize'] |
| 457 |
); |
| 458 |
|
| 459 |
$appendStream = new AppendStream([ |
| 460 |
$cipherTextStream->createStream() |
| 461 |
]); |
| 462 |
|
| 463 |
|
| 464 |
|
| 465 |
|
| 466 |
$cipherOptions['Tag'] = $cipherTextStream->getTag(); |
| 467 |
$appendStream->addStream(Psr7\Utils::streamFor($cipherOptions['Tag'])); |
| 468 |
|
| 469 |
return [base64_encode($commitmentKey), $appendStream]; |
| 470 |
default: |
| 471 |
throw new CryptoException("Unsupported Cipher used for content encryption"); |
| 472 |
} |
| 473 |
} |
| 474 |
} |