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

A128KWAndA128GCMEncryptionWithSpecificProtectedHeaderValuesTest   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 12

Importance

Changes 7
Bugs 4 Features 1
Metric Value
wmc 2
c 7
b 4
f 1
lcom 0
cbo 12
dl 0
loc 111
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A testA128KWAndA128GCMEncryptionWithSpecificProtectedHeaderValues() 0 53 1
A testA128KWAndA128GCMEncryptionWithSpecificProtectedHeaderValuesBis() 0 47 1
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.11
23
 *
24
 * @group RFC7520
25
 */
26
class A128KWAndA128GCMEncryptionWithSpecificProtectedHeaderValuesTest 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 testA128KWAndA128GCMEncryptionWithSpecificProtectedHeaderValues()
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
            'enc' => 'A128GCM',
46
        ];
47
48
        $headers = [
49
            'alg' => 'A128KW',
50
            'kid' => '81b20965-8332-43d9-a468-82160ad91ac8',
51
        ];
52
53
        $expected_flattened_json = '{"protected":"eyJlbmMiOiJBMTI4R0NNIn0","unprotected":{"alg":"A128KW","kid":"81b20965-8332-43d9-a468-82160ad91ac8"},"encrypted_key":"jJIcM9J-hbx3wnqhf5FlkEYos0sHsF0H","iv":"WgEJsDS9bkoXQ3nR","ciphertext":"lIbCyRmRJxnB2yLQOTqjCDKV3H30ossOw3uD9DPsqLL2DM3swKkjOwQyZtWsFLYMj5YeLht_StAn21tHmQJuuNt64T8D4t6C7kC9OCCJ1IHAolUv4MyOt80MoPb8fZYbNKqplzYJgIL58g8N2v46OgyG637d6uuKPwhAnTGm_zWhqc_srOvgiLkzyFXPq1hBAURbc3-8BqeRb48iR1-_5g5UjWVD3lgiLCN_P7AW8mIiFvUNXBPJK3nOWL4teUPS8yHLbWeL83olU4UAgL48x-8dDkH23JykibVSQju-f7e-1xreHWXzWLHs1NqBbre0dEwK3HX_xM0LjUz77Krppgegoutpf5qaKg3l-_xMINmf","tag":"fNYLqpUe84KD45lvDiaBAQ"}';
54
        $expected_json = '{"recipients":[{"encrypted_key":"jJIcM9J-hbx3wnqhf5FlkEYos0sHsF0H"}],"unprotected":{"alg":"A128KW","kid":"81b20965-8332-43d9-a468-82160ad91ac8"},"protected":"eyJlbmMiOiJBMTI4R0NNIn0","iv":"WgEJsDS9bkoXQ3nR","ciphertext":"lIbCyRmRJxnB2yLQOTqjCDKV3H30ossOw3uD9DPsqLL2DM3swKkjOwQyZtWsFLYMj5YeLht_StAn21tHmQJuuNt64T8D4t6C7kC9OCCJ1IHAolUv4MyOt80MoPb8fZYbNKqplzYJgIL58g8N2v46OgyG637d6uuKPwhAnTGm_zWhqc_srOvgiLkzyFXPq1hBAURbc3-8BqeRb48iR1-_5g5UjWVD3lgiLCN_P7AW8mIiFvUNXBPJK3nOWL4teUPS8yHLbWeL83olU4UAgL48x-8dDkH23JykibVSQju-f7e-1xreHWXzWLHs1NqBbre0dEwK3HX_xM0LjUz77Krppgegoutpf5qaKg3l-_xMINmf","tag":"fNYLqpUe84KD45lvDiaBAQ"}';
55
        $expected_iv = 'WgEJsDS9bkoXQ3nR';
56
        $expected_encrypted_key = 'jJIcM9J-hbx3wnqhf5FlkEYos0sHsF0H';
57
        $expected_ciphertext = 'lIbCyRmRJxnB2yLQOTqjCDKV3H30ossOw3uD9DPsqLL2DM3swKkjOwQyZtWsFLYMj5YeLht_StAn21tHmQJuuNt64T8D4t6C7kC9OCCJ1IHAolUv4MyOt80MoPb8fZYbNKqplzYJgIL58g8N2v46OgyG637d6uuKPwhAnTGm_zWhqc_srOvgiLkzyFXPq1hBAURbc3-8BqeRb48iR1-_5g5UjWVD3lgiLCN_P7AW8mIiFvUNXBPJK3nOWL4teUPS8yHLbWeL83olU4UAgL48x-8dDkH23JykibVSQju-f7e-1xreHWXzWLHs1NqBbre0dEwK3HX_xM0LjUz77Krppgegoutpf5qaKg3l-_xMINmf';
58
        $expected_tag = 'fNYLqpUe84KD45lvDiaBAQ';
59
60
        $decrypter = DecrypterFactory::createDecrypter(['A128KW', 'A128GCM']);
61
62
        $loaded_flattened_json = Loader::load($expected_flattened_json);
63
        $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 62 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...
64
65
        $loaded_json = Loader::load($expected_json);
66
        $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 65 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...
67
68
        $this->assertEquals($expected_ciphertext, Base64Url::encode($loaded_flattened_json->getCiphertext()));
69
        $this->assertEquals($protected_headers, $loaded_flattened_json->getSharedProtectedHeaders());
70
        $this->assertEquals($expected_iv, Base64Url::encode($loaded_flattened_json->getIV()));
71
        $this->assertEquals($expected_encrypted_key, Base64Url::encode($loaded_flattened_json->getRecipient(0)->getEncryptedKey()));
72
        $this->assertEquals($headers, $loaded_flattened_json->getSharedHeaders());
73
        $this->assertEquals($expected_tag, Base64Url::encode($loaded_flattened_json->getTag()));
74
75
        $this->assertEquals($expected_ciphertext, Base64Url::encode($loaded_json->getCiphertext()));
76
        $this->assertEquals($protected_headers, $loaded_json->getSharedProtectedHeaders());
77
        $this->assertEquals($expected_iv, Base64Url::encode($loaded_json->getIV()));
78
        $this->assertEquals($expected_encrypted_key, Base64Url::encode($loaded_json->getRecipient(0)->getEncryptedKey()));
79
        $this->assertEquals($headers, $loaded_json->getSharedHeaders());
80
        $this->assertEquals($expected_tag, Base64Url::encode($loaded_json->getTag()));
81
82
        $this->assertEquals($expected_payload, $loaded_flattened_json->getPayload());
83
        $this->assertEquals($expected_payload, $loaded_json->getPayload());
84
    }
85
86
    /**
87
     * Same input as before, but we perform the encryption first.
88
     */
89
    public function testA128KWAndA128GCMEncryptionWithSpecificProtectedHeaderValuesBis()
90
    {
91
        $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.";
92
93
        $private_key = new JWK([
94
            'kty' => 'oct',
95
            'kid' => '81b20965-8332-43d9-a468-82160ad91ac8',
96
            'use' => 'enc',
97
            'alg' => 'A128KW',
98
            'k'   => 'GZy6sIZ6wl9NJOKB-jnmVQ',
99
        ]);
100
101
        $protected_headers = [
102
            'enc' => 'A128GCM',
103
        ];
104
105
        $headers = [
106
            'alg' => 'A128KW',
107
            'kid' => '81b20965-8332-43d9-a468-82160ad91ac8',
108
        ];
109
110
        $jwe = JWEFactory::createJWE($expected_payload, $protected_headers, $headers);
111
        $encrypter = EncrypterFactory::createEncrypter(['A128KW', 'A128GCM']);
112
113
        $jwe = $jwe->addRecipient(
114
            $private_key
115
        );
116
117
        $encrypter->encrypt($jwe);
118
119
        $decrypter = DecrypterFactory::createDecrypter(['A128KW', 'A128GCM']);
120
121
        $loaded_flattened_json = Loader::load($jwe->toFlattenedJSON(0));
122
        $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 121 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...
123
124
        $loaded_json = Loader::load($jwe->toJSON());
125
        $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 124 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...
126
127
        $this->assertEquals($protected_headers, $loaded_flattened_json->getSharedProtectedHeaders());
128
        $this->assertEquals($headers, $loaded_flattened_json->getSharedHeaders());
129
130
        $this->assertEquals($protected_headers, $loaded_json->getSharedProtectedHeaders());
131
        $this->assertEquals($headers, $loaded_json->getSharedHeaders());
132
133
        $this->assertEquals($expected_payload, $loaded_flattened_json->getPayload());
134
        $this->assertEquals($expected_payload, $loaded_json->getPayload());
135
    }
136
}
137