|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Jose\Tests\Component\Encryption; |
| 6 | + |
| 7 | +use Jose\Component\Core\AlgorithmManager; |
| 8 | +use Jose\Component\Core\JWK; |
| 9 | +use Jose\Component\Core\Util\RSAKey; |
| 10 | +use Jose\Component\Encryption\Algorithm\ContentEncryption\A128GCM; |
| 11 | +use Jose\Component\Encryption\Algorithm\KeyEncryption\RSA15; |
| 12 | +use Jose\Component\Encryption\JWEBuilder; |
| 13 | +use Jose\Component\Encryption\JWEDecrypter; |
| 14 | +use Jose\Component\Encryption\Serializer\CompactSerializer; |
| 15 | +use Jose\Component\KeyManagement\JWKFactory; |
| 16 | +use PHPUnit\Framework\Attributes\Test; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | +use function mb_strlen; |
| 19 | +use const E_USER_DEPRECATED; |
| 20 | + |
| 21 | +/** |
| 22 | + * The expected CEK size is passed by the JWEDecrypter as an additional argument of the decryptKey method. |
| 23 | + * Until that argument is part of the KeyEncryption interface (5.0), RSA1_5 falls back to a hardcoded table |
| 24 | + * and triggers a deprecation. |
| 25 | + * |
| 26 | + * @internal |
| 27 | + */ |
| 28 | +final class RSA15ExpectedCekSizeTest extends TestCase |
| 29 | +{ |
| 30 | + #[Test] |
| 31 | + public function aDeprecationIsTriggeredWhenTheExpectedCekSizeIsNotProvided(): void |
| 32 | + { |
| 33 | + $jwk = $this->createKey(); |
| 34 | + $algorithm = new RSA15(); |
| 35 | + $cek = random_bytes(16); // A128GCM CEK |
| 36 | + $header = [ |
| 37 | + 'alg' => 'RSA1_5', |
| 38 | + 'enc' => 'A128GCM', |
| 39 | + ]; |
| 40 | + $additionalHeader = []; |
| 41 | + $encrypted = $algorithm->encryptKey($jwk, $cek, $header, $additionalHeader); |
| 42 | + |
| 43 | + $decrypted = null; |
| 44 | + $deprecations = $this->collectDeprecations(static function () use ( |
| 45 | + $algorithm, |
| 46 | + $jwk, |
| 47 | + $encrypted, |
| 48 | + $header, |
| 49 | + &$decrypted |
| 50 | + ): void { |
| 51 | + $decrypted = $algorithm->decryptKey($jwk, $encrypted, $header); |
| 52 | + }); |
| 53 | + |
| 54 | + static::assertSame($cek, $decrypted); |
| 55 | + static::assertCount(1, $deprecations); |
| 56 | + static::assertStringContainsString( |
| 57 | + 'Calling "Jose\Component\Encryption\Algorithm\KeyEncryption\RSA15::decryptKey()" without the size of the key expected by the content encryption algorithm as fourth argument is deprecated.', |
| 58 | + $deprecations[0] |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + #[Test] |
| 63 | + public function noDeprecationIsTriggeredWhenTheExpectedCekSizeIsProvided(): void |
| 64 | + { |
| 65 | + $jwk = $this->createKey(); |
| 66 | + $algorithm = new RSA15(); |
| 67 | + $cek = random_bytes(16); // A128GCM CEK |
| 68 | + $header = [ |
| 69 | + 'alg' => 'RSA1_5', |
| 70 | + 'enc' => 'A128GCM', |
| 71 | + ]; |
| 72 | + $additionalHeader = []; |
| 73 | + $encrypted = $algorithm->encryptKey($jwk, $cek, $header, $additionalHeader); |
| 74 | + |
| 75 | + $decrypted = null; |
| 76 | + $deprecations = $this->collectDeprecations(static function () use ( |
| 77 | + $algorithm, |
| 78 | + $jwk, |
| 79 | + $encrypted, |
| 80 | + $header, |
| 81 | + &$decrypted |
| 82 | + ): void { |
| 83 | + $decrypted = $algorithm->decryptKey($jwk, $encrypted, $header, 128); |
| 84 | + }); |
| 85 | + |
| 86 | + static::assertSame($cek, $decrypted); |
| 87 | + static::assertSame([], $deprecations); |
| 88 | + } |
| 89 | + |
| 90 | + #[Test] |
| 91 | + public function theProvidedCekSizeIsUsedForTheImplicitRejection(): void |
| 92 | + { |
| 93 | + $jwk = $this->createKey(); |
| 94 | + $algorithm = new RSA15(); |
| 95 | + $key = RSAKey::createFromJWK($jwk); |
| 96 | + $garbage = "\x00" . random_bytes($key->getModulusLength() - 1); |
| 97 | + // The content encryption algorithm is unknown to the hardcoded table. |
| 98 | + $header = [ |
| 99 | + 'alg' => 'RSA1_5', |
| 100 | + 'enc' => 'FOO-256', |
| 101 | + ]; |
| 102 | + |
| 103 | + $result = $algorithm->decryptKey($jwk, $garbage, $header, 256); |
| 104 | + |
| 105 | + static::assertSame(32, mb_strlen($result, '8bit')); |
| 106 | + } |
| 107 | + |
| 108 | + #[Test] |
| 109 | + public function theJweDecrypterProvidesTheExpectedCekSize(): void |
| 110 | + { |
| 111 | + $jwk = $this->createKey(); |
| 112 | + $algorithmManager = new AlgorithmManager([new RSA15(), new A128GCM()]); |
| 113 | + $token = $this->createToken($algorithmManager, $jwk, 'RSA1_5'); |
| 114 | + |
| 115 | + $jwe = (new CompactSerializer())->unserialize($token); |
| 116 | + $decrypter = new JWEDecrypter($algorithmManager); |
| 117 | + |
| 118 | + $deprecations = $this->collectDeprecations(static function () use ($decrypter, $jwe, $jwk): void { |
| 119 | + $jweToDecrypt = $jwe; |
| 120 | + static::assertTrue($decrypter->decryptUsingKey($jweToDecrypt, $jwk, 0)); |
| 121 | + static::assertSame('Live long and prosper.', $jweToDecrypt->getPayload()); |
| 122 | + }); |
| 123 | + |
| 124 | + static::assertSame([], $deprecations); |
| 125 | + } |
| 126 | + |
| 127 | + #[Test] |
| 128 | + public function algorithmsThatDoNotExpectTheCekSizeStillWork(): void |
| 129 | + { |
| 130 | + $jwk = JWKFactory::createOctKey(256, [ |
| 131 | + 'use' => 'enc', |
| 132 | + ]); |
| 133 | + $algorithm = new LegacyKeyEncryptionAlgorithm(); |
| 134 | + $algorithmManager = new AlgorithmManager([$algorithm, new A128GCM()]); |
| 135 | + $token = $this->createToken($algorithmManager, $jwk, $algorithm->name()); |
| 136 | + |
| 137 | + $jwe = (new CompactSerializer())->unserialize($token); |
| 138 | + $decrypter = new JWEDecrypter($algorithmManager); |
| 139 | + |
| 140 | + static::assertTrue($decrypter->decryptUsingKey($jwe, $jwk, 0)); |
| 141 | + static::assertSame('Live long and prosper.', $jwe->getPayload()); |
| 142 | + static::assertSame(4, $algorithm->receivedArgumentCount); |
| 143 | + } |
| 144 | + |
| 145 | + private function createKey(): JWK |
| 146 | + { |
| 147 | + return JWKFactory::createRSAKey(2048, [ |
| 148 | + 'alg' => 'RSA1_5', |
| 149 | + 'use' => 'enc', |
| 150 | + ]); |
| 151 | + } |
| 152 | + |
| 153 | + private function createToken(AlgorithmManager $algorithmManager, JWK $jwk, string $algorithm): string |
| 154 | + { |
| 155 | + $jwe = (new JWEBuilder($algorithmManager)) |
| 156 | + ->create() |
| 157 | + ->withPayload('Live long and prosper.') |
| 158 | + ->withSharedProtectedHeader([ |
| 159 | + 'alg' => $algorithm, |
| 160 | + 'enc' => 'A128GCM', |
| 161 | + ]) |
| 162 | + ->addRecipient($jwk) |
| 163 | + ->build(); |
| 164 | + |
| 165 | + return (new CompactSerializer())->serialize($jwe, 0); |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * @param callable(): void $callback |
| 170 | + * |
| 171 | + * @return list<string> |
| 172 | + */ |
| 173 | + private function collectDeprecations(callable $callback): array |
| 174 | + { |
| 175 | + $deprecations = []; |
| 176 | + set_error_handler(static function (int $errno, string $errstr) use (&$deprecations): bool { |
| 177 | + $deprecations[] = $errstr; |
| 178 | + |
| 179 | + return true; |
| 180 | + }, E_USER_DEPRECATED); |
| 181 | + |
| 182 | + try { |
| 183 | + $callback(); |
| 184 | + } finally { |
| 185 | + restore_error_handler(); |
| 186 | + } |
| 187 | + |
| 188 | + return $deprecations; |
| 189 | + } |
| 190 | +} |
0 commit comments