|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* The MIT License (MIT) |
|
7
|
|
|
* |
|
8
|
|
|
* Copyright (c) 2014-2017 Spomky-Labs |
|
9
|
|
|
* |
|
10
|
|
|
* This software may be modified and distributed under the terms |
|
11
|
|
|
* of the MIT license. See the LICENSE file for details. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Jose\Component\Encryption\Tests; |
|
15
|
|
|
|
|
16
|
|
|
use Jose\Component\Core\JWAManager; |
|
17
|
|
|
use Jose\Component\Encryption\Algorithm\ContentEncryption\A128GCM; |
|
18
|
|
|
use Jose\Component\Encryption\Algorithm\KeyEncryption\ECDHESA128KW; |
|
19
|
|
|
use Jose\Component\Encryption\Compression\CompressionManager; |
|
20
|
|
|
use Jose\Component\Encryption\Compression\Deflate; |
|
21
|
|
|
use Jose\Component\Encryption\Decrypter; |
|
22
|
|
|
use Jose\Component\Encryption\JWELoader; |
|
23
|
|
|
use Jose\Component\Factory\JWEFactory; |
|
24
|
|
|
use Jose\Component\KeyManagement\JWKFactory; |
|
25
|
|
|
use PHPUnit\Framework\TestCase; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* final class ECDHESWithX25519EncryptionTest. |
|
29
|
|
|
* |
|
30
|
|
|
* @group ECDHES |
|
31
|
|
|
* @group Unit |
|
32
|
|
|
*/ |
|
33
|
|
|
final class ECDHESWithX25519EncryptionTest extends TestCase |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @see https://tools.ietf.org/html/rfc7516#appendix-B |
|
37
|
|
|
*/ |
|
38
|
|
|
public function testA128CBCHS256EncryptAndDecrypt() |
|
39
|
|
|
{ |
|
40
|
|
|
$receiverKey = JWKFactory::createOKPKey(['crv' => 'X25519']); |
|
41
|
|
|
$input = "You can trust us to stick with you through thick and thin\xe2\x80\x93to the bitter end. And you can trust us to keep any secret of yours\xe2\x80\x93closer than you keep it yourself. But you cannot trust us to let you face trouble alone, and go off without a word. We are your friends, Frodo."; |
|
42
|
|
|
|
|
43
|
|
|
$protectedHeaders = [ |
|
44
|
|
|
'alg' => 'ECDH-ES+A128KW', |
|
45
|
|
|
'enc' => 'A128GCM', |
|
46
|
|
|
]; |
|
47
|
|
|
|
|
48
|
|
|
$jwt = JWEFactory::createJWEToCompactJSON($input, $receiverKey, $protectedHeaders); |
|
49
|
|
|
|
|
50
|
|
|
|
|
51
|
|
|
$keyEncryptionAlgorithmManager = JWAManager::create([new ECDHESA128KW()]); |
|
52
|
|
|
$contentEncryptionAlgorithmManager = JWAManager::create([new A128GCM()]); |
|
53
|
|
|
$compressionManager = CompressionManager::create([new Deflate()]); |
|
54
|
|
|
$decrypter = new Decrypter($keyEncryptionAlgorithmManager, $contentEncryptionAlgorithmManager, $compressionManager); |
|
55
|
|
|
|
|
56
|
|
|
$jwe = JWELoader::load($jwt); |
|
57
|
|
|
$decrypter->decryptUsingKey($jwe, $receiverKey, $index); |
|
58
|
|
|
$this->assertEquals(0, $index); |
|
59
|
|
|
$this->assertTrue($jwe->hasSharedProtectedHeader('epk')); |
|
60
|
|
|
$this->assertEquals($input, $jwe->getPayload()); |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|