Completed
Push — develop ( b5844e...e46df6 )
by Florent
02:33
created

DirAndA128GCMEncryptionTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 11

Importance

Changes 6
Bugs 3 Features 1
Metric Value
wmc 2
c 6
b 3
f 1
lcom 0
cbo 11
dl 0
loc 98
rs 10
1
<?php
2
3
/*
4
 * The MIT License (MIT)
5
 *
6
 * Copyright (c) 2014-2016 Spomky-Labs
7
 *
8
 * This software may be modified and distributed under the terms
9
 * of the MIT license.  See the LICENSE file for details.
10
 */
11
12
namespace Jose\Test\RFC7520;
13
14
use Base64Url\Base64Url;
15
use Jose\Factory\DecrypterFactory;
16
use Jose\Factory\EncrypterFactory;
17
use Jose\Factory\JWEFactory;
18
use Jose\Loader;
19
use Jose\Object\JWK;
20
21
/**
22
 * @see https://tools.ietf.org/html/rfc7520#section-5.6
23
 *
24
 * @group RFC7520
25
 */
26
class DirAndA128GCMEncryptionTest extends \PHPUnit_Framework_TestCase
27
{
28
    /**
29
     * Please note that we cannot the encryption and get the same result as the example (IV, TAG and other data are always different).
30
     * The output given in the RFC is used and only decrypted.
31
     */
32
    public function testDirAndA128GCMEncryption()
33
    {
34
        $expected_payload = "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.";
35
36
        $private_key = new JWK([
37
            'kty' => 'oct',
38
            'kid' => '77c7e2b8-6e13-45cf-8672-617b5b45243a',
39
            'use' => 'enc',
40
            'alg' => 'A128GCM',
41
            'k'   => 'XctOhJAkA-pD9Lh7ZgW_2A',
42
        ]);
43
44
        $protected_headers = [
45
            'alg' => 'dir',
46
            'kid' => '77c7e2b8-6e13-45cf-8672-617b5b45243a',
47
            'enc' => 'A128GCM',
48
        ];
49
50
        $expected_compact_json = 'eyJhbGciOiJkaXIiLCJraWQiOiI3N2M3ZTJiOC02ZTEzLTQ1Y2YtODY3Mi02MTdiNWI0NTI0M2EiLCJlbmMiOiJBMTI4R0NNIn0..refa467QzzKx6QAB.JW_i_f52hww_ELQPGaYyeAB6HYGcR559l9TYnSovc23XJoBcW29rHP8yZOZG7YhLpT1bjFuvZPjQS-m0IFtVcXkZXdH_lr_FrdYt9HRUYkshtrMmIUAyGmUnd9zMDB2n0cRDIHAzFVeJUDxkUwVAE7_YGRPdcqMyiBoCO-FBdE-Nceb4h3-FtBP-c_BIwCPTjb9o0SbdcdREEMJMyZBH8ySWMVi1gPD9yxi-aQpGbSv_F9N4IZAxscj5g-NJsUPbjk29-s7LJAGb15wEBtXphVCgyy53CoIKLHHeJHXex45Uz9aKZSRSInZI-wjsY0yu3cT4_aQ3i1o-tiE-F8Ios61EKgyIQ4CWao8PFMj8TTnp.vbb32Xvllea2OtmHAdccRQ';
51
        $expected_json = '{"protected":"eyJhbGciOiJkaXIiLCJraWQiOiI3N2M3ZTJiOC02ZTEzLTQ1Y2YtODY3Mi02MTdiNWI0NTI0M2EiLCJlbmMiOiJBMTI4R0NNIn0","iv":"refa467QzzKx6QAB","ciphertext":"JW_i_f52hww_ELQPGaYyeAB6HYGcR559l9TYnSovc23XJoBcW29rHP8yZOZG7YhLpT1bjFuvZPjQS-m0IFtVcXkZXdH_lr_FrdYt9HRUYkshtrMmIUAyGmUnd9zMDB2n0cRDIHAzFVeJUDxkUwVAE7_YGRPdcqMyiBoCO-FBdE-Nceb4h3-FtBP-c_BIwCPTjb9o0SbdcdREEMJMyZBH8ySWMVi1gPD9yxi-aQpGbSv_F9N4IZAxscj5g-NJsUPbjk29-s7LJAGb15wEBtXphVCgyy53CoIKLHHeJHXex45Uz9aKZSRSInZI-wjsY0yu3cT4_aQ3i1o-tiE-F8Ios61EKgyIQ4CWao8PFMj8TTnp","tag":"vbb32Xvllea2OtmHAdccRQ"}';
52
        $expected_iv = 'refa467QzzKx6QAB';
53
        $expected_ciphertext = 'JW_i_f52hww_ELQPGaYyeAB6HYGcR559l9TYnSovc23XJoBcW29rHP8yZOZG7YhLpT1bjFuvZPjQS-m0IFtVcXkZXdH_lr_FrdYt9HRUYkshtrMmIUAyGmUnd9zMDB2n0cRDIHAzFVeJUDxkUwVAE7_YGRPdcqMyiBoCO-FBdE-Nceb4h3-FtBP-c_BIwCPTjb9o0SbdcdREEMJMyZBH8ySWMVi1gPD9yxi-aQpGbSv_F9N4IZAxscj5g-NJsUPbjk29-s7LJAGb15wEBtXphVCgyy53CoIKLHHeJHXex45Uz9aKZSRSInZI-wjsY0yu3cT4_aQ3i1o-tiE-F8Ios61EKgyIQ4CWao8PFMj8TTnp';
54
        $expected_tag = 'vbb32Xvllea2OtmHAdccRQ';
55
56
        $decrypter = DecrypterFactory::createDecrypter(['dir', 'A128GCM']);
57
58
        $loaded_compact_json = Loader::load($expected_compact_json);
59
        $decrypter->decryptUsingKey($loaded_compact_json, $private_key);
60
61
        $loaded_json = Loader::load($expected_json);
62
        $decrypter->decryptUsingKey($loaded_json, $private_key);
63
64
        $this->assertEquals($expected_ciphertext, Base64Url::encode($loaded_compact_json->getCiphertext()));
65
        $this->assertEquals($protected_headers, $loaded_compact_json->getSharedProtectedHeaders());
66
        $this->assertEquals($expected_iv, Base64Url::encode($loaded_compact_json->getIV()));
67
        $this->assertEquals($expected_tag, Base64Url::encode($loaded_compact_json->getTag()));
68
69
        $this->assertEquals($expected_ciphertext, Base64Url::encode($loaded_json->getCiphertext()));
70
        $this->assertEquals($protected_headers, $loaded_json->getSharedProtectedHeaders());
71
        $this->assertEquals($expected_iv, Base64Url::encode($loaded_json->getIV()));
72
        $this->assertEquals($expected_tag, Base64Url::encode($loaded_json->getTag()));
73
74
        $this->assertEquals($expected_payload, $loaded_compact_json->getPayload());
75
        $this->assertEquals($expected_payload, $loaded_json->getPayload());
76
    }
77
78
    /**
79
     * Same input as before, but we perform the encryption first.
80
     */
81
    public function testDirAndA128GCMEncryptionBis()
82
    {
83
        $expected_payload = "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.";
84
85
        $private_key = new JWK([
86
            'kty' => 'oct',
87
            'kid' => '77c7e2b8-6e13-45cf-8672-617b5b45243a',
88
            'use' => 'enc',
89
            'alg' => 'A128GCM',
90
            'k'   => 'XctOhJAkA-pD9Lh7ZgW_2A',
91
        ]);
92
93
        $protected_headers = [
94
            'alg' => 'dir',
95
            'kid' => '77c7e2b8-6e13-45cf-8672-617b5b45243a',
96
            'enc' => 'A128GCM',
97
        ];
98
99
        $jwe = JWEFactory::createJWE($expected_payload, $protected_headers);
100
        $encrypter = EncrypterFactory::createEncrypter(['dir', 'A128GCM']);
101
102
        $jwe = $jwe->addRecipient(
103
            $private_key
104
        );
105
106
        $encrypter->encrypt($jwe);
107
108
        $decrypter = DecrypterFactory::createDecrypter(['dir', 'A128GCM']);
109
110
        $loaded_compact_json = Loader::load($jwe->toCompactJSON(0));
111
        $decrypter->decryptUsingKey($loaded_compact_json, $private_key);
112
113
        $loaded_json = Loader::load($jwe->toJSON());
114
        $decrypter->decryptUsingKey($loaded_json, $private_key);
115
116
        $this->assertEquals($protected_headers, $loaded_compact_json->getSharedProtectedHeaders());
117
118
        $this->assertEquals($protected_headers, $loaded_json->getSharedProtectedHeaders());
119
120
        $this->assertEquals($expected_payload, $loaded_compact_json->getPayload());
121
        $this->assertEquals($expected_payload, $loaded_json->getPayload());
122
    }
123
}
124