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

testA128KWAndA128GCMEncryptionWithAdditionalAuthenticatedData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 52
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 3 Features 1
Metric Value
c 5
b 3
f 1
dl 0
loc 52
rs 9.4929
cc 1
eloc 39
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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.10
23
 *
24
 * @group RFC7520
25
 */
26
class A128KWAndA128GCMEncryptionWithAdditionalAuthenticatedDataTest 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 testA128KWAndA128GCMEncryptionWithAdditionalAuthenticatedData()
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' => '81b20965-8332-43d9-a468-82160ad91ac8',
39
            'use' => 'enc',
40
            'alg' => 'A128KW',
41
            'k'   => 'GZy6sIZ6wl9NJOKB-jnmVQ',
42
        ]);
43
44
        $protected_headers = [
45
            'alg' => 'A128KW',
46
            'kid' => '81b20965-8332-43d9-a468-82160ad91ac8',
47
            'enc' => 'A128GCM',
48
        ];
49
50
        $expected_flattened_json = '{"protected":"eyJhbGciOiJBMTI4S1ciLCJraWQiOiI4MWIyMDk2NS04MzMyLTQzZDktYTQ2OC04MjE2MGFkOTFhYzgiLCJlbmMiOiJBMTI4R0NNIn0","encrypted_key":"4YiiQ_ZzH76TaIkJmYfRFgOV9MIpnx4X","aad":"WyJ2Y2FyZCIsW1sidmVyc2lvbiIse30sInRleHQiLCI0LjAiXSxbImZuIix7fSwidGV4dCIsIk1lcmlhZG9jIEJyYW5keWJ1Y2siXSxbIm4iLHt9LCJ0ZXh0IixbIkJyYW5keWJ1Y2siLCJNZXJpYWRvYyIsIk1yLiIsIiJdXSxbImJkYXkiLHt9LCJ0ZXh0IiwiVEEgMjk4MiJdLFsiZ2VuZGVyIix7fSwidGV4dCIsIk0iXV1d","iv":"veCx9ece2orS7c_N","ciphertext":"Z_3cbr0k3bVM6N3oSNmHz7Lyf3iPppGf3Pj17wNZqteJ0Ui8p74SchQP8xygM1oFRWCNzeIa6s6BcEtp8qEFiqTUEyiNkOWDNoF14T_4NFqF-p2Mx8zkbKxI7oPK8KNarFbyxIDvICNqBLba-v3uzXBdB89fzOI-Lv4PjOFAQGHrgv1rjXAmKbgkft9cB4WeyZw8MldbBhc-V_KWZslrsLNygon_JJWd_ek6LQn5NRehvApqf9ZrxB4aq3FXBxOxCys35PhCdaggy2kfUfl2OkwKnWUbgXVD1C6HxLIlqHhCwXDG59weHrRDQeHyMRoBljoV3X_bUTJDnKBFOod7nLz-cj48JMx3SnCZTpbQAkFV","tag":"vOaH_Rajnpy_3hOtqvZHRA"}';
51
        $expected_json = '{"recipients":[{"encrypted_key":"4YiiQ_ZzH76TaIkJmYfRFgOV9MIpnx4X"}],"protected":"eyJhbGciOiJBMTI4S1ciLCJraWQiOiI4MWIyMDk2NS04MzMyLTQzZDktYTQ2OC04MjE2MGFkOTFhYzgiLCJlbmMiOiJBMTI4R0NNIn0","iv":"veCx9ece2orS7c_N","aad":"WyJ2Y2FyZCIsW1sidmVyc2lvbiIse30sInRleHQiLCI0LjAiXSxbImZuIix7fSwidGV4dCIsIk1lcmlhZG9jIEJyYW5keWJ1Y2siXSxbIm4iLHt9LCJ0ZXh0IixbIkJyYW5keWJ1Y2siLCJNZXJpYWRvYyIsIk1yLiIsIiJdXSxbImJkYXkiLHt9LCJ0ZXh0IiwiVEEgMjk4MiJdLFsiZ2VuZGVyIix7fSwidGV4dCIsIk0iXV1d","ciphertext":"Z_3cbr0k3bVM6N3oSNmHz7Lyf3iPppGf3Pj17wNZqteJ0Ui8p74SchQP8xygM1oFRWCNzeIa6s6BcEtp8qEFiqTUEyiNkOWDNoF14T_4NFqF-p2Mx8zkbKxI7oPK8KNarFbyxIDvICNqBLba-v3uzXBdB89fzOI-Lv4PjOFAQGHrgv1rjXAmKbgkft9cB4WeyZw8MldbBhc-V_KWZslrsLNygon_JJWd_ek6LQn5NRehvApqf9ZrxB4aq3FXBxOxCys35PhCdaggy2kfUfl2OkwKnWUbgXVD1C6HxLIlqHhCwXDG59weHrRDQeHyMRoBljoV3X_bUTJDnKBFOod7nLz-cj48JMx3SnCZTpbQAkFV","tag":"vOaH_Rajnpy_3hOtqvZHRA"}';
52
        $expected_cek = '75m1ALsYv10pZTKPWrsqdg';
0 ignored issues
show
Unused Code introduced by
$expected_cek is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
53
        $expected_iv = 'veCx9ece2orS7c_N';
54
        $expected_aad = 'WyJ2Y2FyZCIsW1sidmVyc2lvbiIse30sInRleHQiLCI0LjAiXSxbImZuIix7fSwidGV4dCIsIk1lcmlhZG9jIEJyYW5keWJ1Y2siXSxbIm4iLHt9LCJ0ZXh0IixbIkJyYW5keWJ1Y2siLCJNZXJpYWRvYyIsIk1yLiIsIiJdXSxbImJkYXkiLHt9LCJ0ZXh0IiwiVEEgMjk4MiJdLFsiZ2VuZGVyIix7fSwidGV4dCIsIk0iXV1d';
55
        $expected_encrypted_key = '4YiiQ_ZzH76TaIkJmYfRFgOV9MIpnx4X';
56
        $expected_ciphertext = 'Z_3cbr0k3bVM6N3oSNmHz7Lyf3iPppGf3Pj17wNZqteJ0Ui8p74SchQP8xygM1oFRWCNzeIa6s6BcEtp8qEFiqTUEyiNkOWDNoF14T_4NFqF-p2Mx8zkbKxI7oPK8KNarFbyxIDvICNqBLba-v3uzXBdB89fzOI-Lv4PjOFAQGHrgv1rjXAmKbgkft9cB4WeyZw8MldbBhc-V_KWZslrsLNygon_JJWd_ek6LQn5NRehvApqf9ZrxB4aq3FXBxOxCys35PhCdaggy2kfUfl2OkwKnWUbgXVD1C6HxLIlqHhCwXDG59weHrRDQeHyMRoBljoV3X_bUTJDnKBFOod7nLz-cj48JMx3SnCZTpbQAkFV';
57
        $expected_tag = 'vOaH_Rajnpy_3hOtqvZHRA';
58
59
        $decrypter = DecrypterFactory::createDecrypter(['A128KW', 'A128GCM']);
60
61
        $loaded_flattened_json = Loader::load($expected_flattened_json);
62
        $decrypter->decryptUsingKey($loaded_flattened_json, $private_key);
0 ignored issues
show
Bug introduced by
It seems like $loaded_flattened_json defined by \Jose\Loader::load($expected_flattened_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
        $loaded_json = Loader::load($expected_json);
65
        $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 64 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...
66
67
        $this->assertEquals($expected_ciphertext, Base64Url::encode($loaded_flattened_json->getCiphertext()));
68
        $this->assertEquals($protected_headers, $loaded_flattened_json->getSharedProtectedHeaders());
69
        $this->assertEquals($expected_iv, Base64Url::encode($loaded_flattened_json->getIV()));
70
        $this->assertEquals($expected_encrypted_key, Base64Url::encode($loaded_flattened_json->getRecipient(0)->getEncryptedKey()));
71
        $this->assertEquals($expected_tag, Base64Url::encode($loaded_flattened_json->getTag()));
72
        $this->assertEquals($expected_aad, $loaded_flattened_json->getAAD());
73
74
        $this->assertEquals($expected_ciphertext, Base64Url::encode($loaded_json->getCiphertext()));
75
        $this->assertEquals($protected_headers, $loaded_json->getSharedProtectedHeaders());
76
        $this->assertEquals($expected_iv, Base64Url::encode($loaded_json->getIV()));
77
        $this->assertEquals($expected_encrypted_key, Base64Url::encode($loaded_json->getRecipient(0)->getEncryptedKey()));
78
        $this->assertEquals($expected_tag, Base64Url::encode($loaded_json->getTag()));
79
        $this->assertEquals($expected_aad, $loaded_json->getAAD());
80
81
        $this->assertEquals($expected_payload, $loaded_flattened_json->getPayload());
82
        $this->assertEquals($expected_payload, $loaded_json->getPayload());
83
    }
84
85
    /**
86
     * Same input as before, but we perform the encryption first.
87
     */
88
    public function testA128KWAndA128GCMEncryptionWithAdditionalAuthenticatedDataBis()
89
    {
90
        $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.";
91
92
        $private_key = new JWK([
93
            'kty' => 'oct',
94
            'kid' => '81b20965-8332-43d9-a468-82160ad91ac8',
95
            'use' => 'enc',
96
            'alg' => 'A128KW',
97
            'k'   => 'GZy6sIZ6wl9NJOKB-jnmVQ',
98
        ]);
99
100
        $protected_headers = [
101
            'alg' => 'A128KW',
102
            'kid' => '81b20965-8332-43d9-a468-82160ad91ac8',
103
            'enc' => 'A128GCM',
104
        ];
105
106
        $jwe = JWEFactory::createJWE($expected_payload, $protected_headers);
107
        $encrypter = EncrypterFactory::createEncrypter(['A128KW', 'A128GCM']);
108
109
        $jwe = $jwe->addRecipient(
110
            $private_key
111
        );
112
113
        $encrypter->encrypt($jwe);
114
115
        $decrypter = DecrypterFactory::createDecrypter(['A128KW', 'A128GCM']);
116
117
        $loaded_flattened_json = Loader::load($jwe->toFlattenedJSON(0));
118
        $decrypter->decryptUsingKey($loaded_flattened_json, $private_key);
0 ignored issues
show
Bug introduced by
It seems like $loaded_flattened_json defined by \Jose\Loader::load($jwe->toFlattenedJSON(0)) on line 117 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...
119
120
        $loaded_json = Loader::load($jwe->toJSON());
121
        $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 120 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...
122
123
        $this->assertEquals($protected_headers, $loaded_flattened_json->getSharedProtectedHeaders());
124
125
        $this->assertEquals($protected_headers, $loaded_json->getSharedProtectedHeaders());
126
127
        $this->assertEquals($expected_payload, $loaded_flattened_json->getPayload());
128
        $this->assertEquals($expected_payload, $loaded_json->getPayload());
129
    }
130
}
131