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

testA128KWAndA128GCMEncryptionBis()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 48
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
c 4
b 1
f 0
dl 0
loc 48
rs 9.125
cc 1
eloc 30
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.8
23
 *
24
 * @group RFC7520
25
 */
26
class A128KWAndA128GCMEncryptionTest 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 testA128KWAndA128GCMEncryption()
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_compact_json = 'eyJhbGciOiJBMTI4S1ciLCJraWQiOiI4MWI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Yw0FkQZF","tag":"ER7MWJZ1FBI_NKvn7Zb1Lw"}';
52
        $expected_json = '{"recipients":[{"encrypted_key":"CBI6oDw8MydIx1IBntf_lQcw2MmJKIQx"}],"protected":"eyJhbGciOiJBMTI4S1ciLCJraWQiOiI4MWIyMDk2NS04MzMyLTQzZDktYTQ2OC04MjE2MGFkOTFhYzgiLCJlbmMiOiJBMTI4R0NNIn0","iv":"Qx0pmsDa8KnJc9Jo","ciphertext":"AwliP-KmWgsZ37BvzCefNen6VTbRK3QMA4TkvRkH0tP1bTdhtFJgJxeVmJkLD61A1hnWGetdg11c9ADsnWgL56NyxwSYjU1ZEHcGkd3EkU0vjHi9gTlb90qSYFfeF0LwkcTtjbYKCsiNJQkcIp1yeM03OmuiYSoYJVSpf7ej6zaYcMv3WwdxDFl8REwOhNImk2Xld2JXq6BR53TSFkyT7PwVLuq-1GwtGHlQeg7gDT6xW0JqHDPn_H-puQsmthc9Zg0ojmJfqqFvETUxLAF-KjcBTS5dNy6egwkYtOt8EIHK-oEsKYtZRaa8Z7MOZ7UGxGIMvEmxrGCPeJa14slv2-gaqK0kEThkaSqdYw0FkQZF","tag":"ER7MWJZ1FBI_NKvn7Zb1Lw"}';
53
        $expected_cek = 'aY5_Ghmk9KxWPBLu_glx1w';
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...
54
        $expected_iv = 'Qx0pmsDa8KnJc9Jo';
55
        $expected_encrypted_key = 'CBI6oDw8MydIx1IBntf_lQcw2MmJKIQx';
56
        $expected_ciphertext = 'AwliP-KmWgsZ37BvzCefNen6VTbRK3QMA4TkvRkH0tP1bTdhtFJgJxeVmJkLD61A1hnWGetdg11c9ADsnWgL56NyxwSYjU1ZEHcGkd3EkU0vjHi9gTlb90qSYFfeF0LwkcTtjbYKCsiNJQkcIp1yeM03OmuiYSoYJVSpf7ej6zaYcMv3WwdxDFl8REwOhNImk2Xld2JXq6BR53TSFkyT7PwVLuq-1GwtGHlQeg7gDT6xW0JqHDPn_H-puQsmthc9Zg0ojmJfqqFvETUxLAF-KjcBTS5dNy6egwkYtOt8EIHK-oEsKYtZRaa8Z7MOZ7UGxGIMvEmxrGCPeJa14slv2-gaqK0kEThkaSqdYw0FkQZF';
57
        $expected_tag = 'ER7MWJZ1FBI_NKvn7Zb1Lw';
58
59
        $decrypter = DecrypterFactory::createDecrypter(['A128KW', 'A128GCM']);
60
61
        $loaded_compact_json = Loader::load($expected_compact_json);
62
        $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 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_flattened_json = Loader::load($expected_flattened_json);
65
        $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 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
        $loaded_json = Loader::load($expected_json);
68
        $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 67 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...
69
70
        $this->assertEquals($expected_ciphertext, Base64Url::encode($loaded_compact_json->getCiphertext()));
71
        $this->assertEquals($protected_headers, $loaded_compact_json->getSharedProtectedHeaders());
72
        $this->assertEquals($expected_iv, Base64Url::encode($loaded_compact_json->getIV()));
73
        $this->assertEquals($expected_encrypted_key, Base64Url::encode($loaded_compact_json->getRecipient(0)->getEncryptedKey()));
74
        $this->assertEquals($expected_tag, Base64Url::encode($loaded_compact_json->getTag()));
75
76
        $this->assertEquals($expected_ciphertext, Base64Url::encode($loaded_flattened_json->getCiphertext()));
77
        $this->assertEquals($protected_headers, $loaded_flattened_json->getSharedProtectedHeaders());
78
        $this->assertEquals($expected_iv, Base64Url::encode($loaded_flattened_json->getIV()));
79
        $this->assertEquals($expected_encrypted_key, Base64Url::encode($loaded_flattened_json->getRecipient(0)->getEncryptedKey()));
80
        $this->assertEquals($expected_tag, Base64Url::encode($loaded_flattened_json->getTag()));
81
82
        $this->assertEquals($expected_ciphertext, Base64Url::encode($loaded_json->getCiphertext()));
83
        $this->assertEquals($protected_headers, $loaded_json->getSharedProtectedHeaders());
84
        $this->assertEquals($expected_iv, Base64Url::encode($loaded_json->getIV()));
85
        $this->assertEquals($expected_encrypted_key, Base64Url::encode($loaded_json->getRecipient(0)->getEncryptedKey()));
86
        $this->assertEquals($expected_tag, Base64Url::encode($loaded_json->getTag()));
87
88
        $this->assertEquals($expected_payload, $loaded_compact_json->getPayload());
89
        $this->assertEquals($expected_payload, $loaded_flattened_json->getPayload());
90
        $this->assertEquals($expected_payload, $loaded_json->getPayload());
91
    }
92
93
    /**
94
     * Same input as before, but we perform the encryption first.
95
     */
96
    public function testA128KWAndA128GCMEncryptionBis()
97
    {
98
        $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.";
99
100
        $private_key = new JWK([
101
            'kty' => 'oct',
102
            'kid' => '81b20965-8332-43d9-a468-82160ad91ac8',
103
            'use' => 'enc',
104
            'alg' => 'A128KW',
105
            'k'   => 'GZy6sIZ6wl9NJOKB-jnmVQ',
106
        ]);
107
108
        $protected_headers = [
109
            'alg' => 'A128KW',
110
            'kid' => '81b20965-8332-43d9-a468-82160ad91ac8',
111
            'enc' => 'A128GCM',
112
        ];
113
114
        $jwe = JWEFactory::createJWE($expected_payload, $protected_headers);
115
        $encrypter = EncrypterFactory::createEncrypter(['A128KW', 'A128GCM']);
116
117
        $jwe = $jwe->addRecipient(
118
            $private_key
119
        );
120
121
        $encrypter->encrypt($jwe);
122
123
        $decrypter = DecrypterFactory::createDecrypter(['A128KW', 'A128GCM']);
124
125
        $loaded_compact_json = Loader::load($jwe->toCompactJSON(0));
126
        $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 125 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...
127
128
        $loaded_flattened_json = Loader::load($jwe->toFlattenedJSON(0));
129
        $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 128 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...
130
131
        $loaded_json = Loader::load($jwe->toJSON());
132
        $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 131 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...
133
134
        $this->assertEquals($protected_headers, $loaded_compact_json->getSharedProtectedHeaders());
135
136
        $this->assertEquals($protected_headers, $loaded_flattened_json->getSharedProtectedHeaders());
137
138
        $this->assertEquals($protected_headers, $loaded_json->getSharedProtectedHeaders());
139
140
        $this->assertEquals($expected_payload, $loaded_compact_json->getPayload());
141
        $this->assertEquals($expected_payload, $loaded_flattened_json->getPayload());
142
        $this->assertEquals($expected_payload, $loaded_json->getPayload());
143
    }
144
}
145