GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

AESKWAlgorithm   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 109
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
eloc 25
dl 0
loc 109
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 2
A _decryptKey() 0 3 1
A _encryptKey() 0 3 1
A _kw() 0 6 2
A headerParameters() 0 4 1
A fromJWK() 0 9 2
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWE\KeyAlgorithm;
6
7
use Sop\AESKW\AESKeyWrapAlgorithm;
8
use Sop\JWX\JWA\JWA;
9
use Sop\JWX\JWE\KeyAlgorithm\Feature\RandomCEK;
10
use Sop\JWX\JWE\KeyManagementAlgorithm;
11
use Sop\JWX\JWK\JWK;
12
use Sop\JWX\JWK\Symmetric\SymmetricKeyJWK;
13
use Sop\JWX\JWT\Header\Header;
14
use Sop\JWX\JWT\Parameter\AlgorithmParameter;
15
16
/**
17
 * Base class for algorithms implementing AES key wrap.
18
 *
19
 * @see https://tools.ietf.org/html/rfc7518#section-4.4
20
 */
21
abstract class AESKWAlgorithm extends KeyManagementAlgorithm
22
{
23
    use RandomCEK;
24
25
    /**
26
     * Mapping from algorithm name to class name.
27
     *
28
     * @internal
29
     *
30
     * @var array
31
     */
32
    public const MAP_ALGO_TO_CLASS = [
33
        JWA::ALGO_A128KW => A128KWAlgorithm::class,
34
        JWA::ALGO_A192KW => A192KWAlgorithm::class,
35
        JWA::ALGO_A256KW => A256KWAlgorithm::class,
36
    ];
37
38
    /**
39
     * Key encryption key.
40
     *
41
     * @var string
42
     */
43
    protected $_kek;
44
45
    /**
46
     * Key wrapping algorithm.
47
     *
48
     * Lazily initialized.
49
     *
50
     * @var null|AESKeyWrapAlgorithm
51
     */
52
    protected $_kw;
53
54
    /**
55
     * Constructor.
56
     *
57
     * @param string $kek Key encryption key
58
     */
59 24
    public function __construct(string $kek)
60
    {
61 24
        if (strlen($kek) !== $this->_kekSize()) {
62 1
            throw new \LengthException(
63 1
                'Key encryption key must be ' . $this->_kekSize() . ' bytes.');
64
        }
65 23
        $this->_kek = $kek;
66 23
    }
67
68
    /**
69
     * Initialize from JWK.
70
     *
71
     * @throws \UnexpectedValueException
72
     *
73
     * @return self
74
     */
75 9
    public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm
76
    {
77 9
        $jwk = SymmetricKeyJWK::fromJWK($jwk);
78 9
        $alg = JWA::deriveAlgorithmName($header, $jwk);
79 8
        if (!array_key_exists($alg, self::MAP_ALGO_TO_CLASS)) {
80 1
            throw new \UnexpectedValueException("Unsupported algorithm '{$alg}'.");
81
        }
82 7
        $cls = self::MAP_ALGO_TO_CLASS[$alg];
83 7
        return new $cls($jwk->key());
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89 3
    public function headerParameters(): array
90
    {
91 3
        return array_merge(parent::headerParameters(),
92 3
            [AlgorithmParameter::fromAlgorithm($this)]);
93
    }
94
95
    /**
96
     * Get the size of the key encryption key in bytes.
97
     */
98
    abstract protected function _kekSize(): int;
99
100
    /**
101
     * Get key wrapping algorithm instance.
102
     */
103
    abstract protected function _AESKWAlgo(): AESKeyWrapAlgorithm;
104
105
    /**
106
     * Get key wrapping algorithm.
107
     */
108 24
    protected function _kw(): AESKeyWrapAlgorithm
109
    {
110 24
        if (!isset($this->_kw)) {
111 20
            $this->_kw = $this->_AESKWAlgo();
112
        }
113 24
        return $this->_kw;
114
    }
115
116
    /**
117
     * {@inheritdoc}
118
     */
119 18
    protected function _encryptKey(string $key, Header &$header): string
120
    {
121 18
        return $this->_kw()->wrap($key, $this->_kek);
122
    }
123
124
    /**
125
     * {@inheritdoc}
126
     */
127 15
    protected function _decryptKey(string $ciphertext, Header $header): string
128
    {
129 15
        return $this->_kw()->unwrap($ciphertext, $this->_kek);
130
    }
131
}
132