| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
namespace Jose\Component\Encryption\Algorithm\KeyEncryption; |
| 6 |
|
| 7 |
use InvalidArgumentException; |
| 8 |
use Jose\Component\Core\JWK; |
| 9 |
use Jose\Component\Core\Util\RSAKey; |
| 10 |
use Jose\Component\Encryption\Algorithm\KeyEncryption\Util\RSACrypt; |
| 11 |
use function is_string; |
| 12 |
|
| 13 |
final class RSA15 extends RSA |
| 14 |
{ |
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
private const CEK_LENGTHS = [ |
| 19 |
'A128GCM' => 16, |
| 20 |
'A192GCM' => 24, |
| 21 |
'A256GCM' => 32, |
| 22 |
'A128CBC-HS256' => 32, |
| 23 |
'A192CBC-HS384' => 48, |
| 24 |
'A256CBC-HS512' => 64, |
| 25 |
]; |
| 26 |
|
| 27 |
public function name(): string |
| 28 |
{ |
| 29 |
return 'RSA1_5'; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public function decryptKey(JWK $key, string $encrypted_cek, array $header): string |
| 36 |
{ |
| 37 |
$this->checkKey($key); |
| 38 |
if (! $key->has('d')) { |
| 39 |
throw new InvalidArgumentException('The key is not a private key'); |
| 40 |
} |
| 41 |
$priv = RSAKey::createFromJWK($key); |
| 42 |
|
| 43 |
return RSACrypt::decrypt( |
| 44 |
$priv, |
| 45 |
$encrypted_cek, |
| 46 |
RSACrypt::ENCRYPTION_PKCS1, |
| 47 |
null, |
| 48 |
$this->getExpectedCekLength($header) |
| 49 |
); |
| 50 |
} |
| 51 |
|
| 52 |
protected function getEncryptionMode(): int |
| 53 |
{ |
| 54 |
return RSACrypt::ENCRYPTION_PKCS1; |
| 55 |
} |
| 56 |
|
| 57 |
protected function getHashAlgorithm(): ?string |
| 58 |
{ |
| 59 |
return null; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
private function getExpectedCekLength(array $header): ?int |
| 66 |
{ |
| 67 |
$enc = $header['enc'] ?? null; |
| 68 |
if (! is_string($enc)) { |
| 69 |
return null; |
| 70 |
} |
| 71 |
|
| 72 |
return self::CEK_LENGTHS[$enc] ?? null; |
| 73 |
} |
| 74 |
} |