Completed
Push — develop ( 686594...b5844e )
by Florent
03:11
created

testDirAndA128GCMEncryptionBis()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 42
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 42
rs 8.8571
cc 1
eloc 26
nc 1
nop 0
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);
0 ignored issues
show
Bug introduced by
It seems like $loaded_compact_json defined by \Jose\Loader::load($expected_compact_json) on line 58 can also be of type object<Jose\Object\JWSInterface>; however, Jose\Decrypter::decryptUsingKey() does only seem to accept object<Jose\Object\JWEInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
60
61
        $loaded_json = Loader::load($expected_json);
62
        $decrypter->decryptUsingKey($loaded_json, $private_key);
0 ignored issues
show
Bug introduced by
It seems like $loaded_json defined by \Jose\Loader::load($expected_json) on line 61 can also be of type object<Jose\Object\JWSInterface>; however, Jose\Decrypter::decryptUsingKey() does only seem to accept object<Jose\Object\JWEInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $loaded_compact_json defined by \Jose\Loader::load($jwe->toCompactJSON(0)) on line 110 can also be of type object<Jose\Object\JWSInterface>; however, Jose\Decrypter::decryptUsingKey() does only seem to accept object<Jose\Object\JWEInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
112
113
        $loaded_json = Loader::load($jwe->toJSON());
114
        $decrypter->decryptUsingKey($loaded_json, $private_key);
0 ignored issues
show
Bug introduced by
It seems like $loaded_json defined by \Jose\Loader::load($jwe->toJSON()) on line 113 can also be of type object<Jose\Object\JWSInterface>; however, Jose\Decrypter::decryptUsingKey() does only seem to accept object<Jose\Object\JWEInterface>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
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