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.

KeyManagementAlgorithm   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 17
dl 0
loc 116
ccs 20
cts 20
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A encrypt() 0 6 2
A decrypt() 0 6 2
A headerParameters() 0 7 2
A withKeyID() 0 5 1
A fromJWK() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Sop\JWX\JWE;
6
7
use Sop\JWX\JWE\KeyAlgorithm\KeyAlgorithmFactory;
8
use Sop\JWX\JWK\JWK;
9
use Sop\JWX\JWT\Header\Header;
10
use Sop\JWX\JWT\Header\HeaderParameters;
11
use Sop\JWX\JWT\Parameter\AlgorithmParameterValue;
12
use Sop\JWX\JWT\Parameter\KeyIDParameter;
13
14
/**
15
 * Base class for algorithms used for CEK management for the content encryption
16
 * algorithms.
17
 */
18
abstract class KeyManagementAlgorithm implements AlgorithmParameterValue, HeaderParameters
19
{
20
    /**
21
     * ID of the key used by the algorithm.
22
     *
23
     * If set, KeyID parameter shall be automatically inserted into JWE's
24
     * header.
25
     *
26
     * @var null|string
27
     */
28
    protected $_keyID;
29
30
    /**
31
     * Encrypt a key to be inserted into JWE header.
32
     *
33
     * @param string      $cek    Content encryption key
34
     * @param null|Header $header Optional reference to the Header variable,
35
     *                            which may be updated to contain parameters
36
     *                            specific to this encrypt invocation.
37
     *                            If the variable is referenced, but is a null,
38
     *                            it shall be initialized to an empty Header.
39
     *
40
     * @throws \RuntimeException For generic errors
41
     *
42
     * @return string Encrypted key
43
     */
44 49
    final public function encrypt(string $cek, Header &$header = null): string
45
    {
46 49
        if (!isset($header)) {
47 34
            $header = new Header();
48
        }
49 49
        return $this->_encryptKey($cek, $header);
50
    }
51
52
    /**
53
     * Decrypt a CEK from the encrypted data.
54
     *
55
     * @param string      $data   Encrypted key
56
     * @param null|Header $header Optional header containing parameters
57
     *                            required to decrypt the key
58
     *
59
     * @throws \RuntimeException For generic errors
60
     *
61
     * @return string Content encryption key
62
     */
63 44
    final public function decrypt(string $data, ?Header $header = null): string
64
    {
65 44
        if (!isset($header)) {
66 27
            $header = new Header();
67
        }
68 44
        return $this->_decryptKey($data, $header);
69
    }
70
71
    /**
72
     * Get content encryption key for the encryption.
73
     *
74
     * Returned key may be random depending on the key management algorithm.
75
     *
76
     * @param int $length Required key size in bytes
77
     */
78
    abstract public function cekForEncryption(int $length): string;
79
80
    /**
81
     * Initialize key management algorithm from a JWK and a header.
82
     */
83 3
    public static function fromJWK(JWK $jwk, Header $header): KeyManagementAlgorithm
84
    {
85 3
        $factory = new KeyAlgorithmFactory($header);
86 3
        return $factory->algoByKey($jwk);
87
    }
88
89
    /**
90
     * Get self with key ID.
91
     *
92
     * @param null|string $id Key ID or null to remove
93
     */
94 3
    public function withKeyID(?string $id): self
95
    {
96 3
        $obj = clone $this;
97 3
        $obj->_keyID = $id;
98 3
        return $obj;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 19
    public function headerParameters(): array
105
    {
106 19
        $params = [];
107 19
        if (isset($this->_keyID)) {
108 3
            $params[] = new KeyIDParameter($this->_keyID);
109
        }
110 19
        return $params;
111
    }
112
113
    /**
114
     * Encrypt a key.
115
     *
116
     * @param string $key    Key to be encrypted
117
     * @param Header $header Reference to the Header variable, that shall
118
     *                       be updated to contain parameters specific to the encryption
119
     *
120
     * @return string Ciphertext
121
     */
122
    abstract protected function _encryptKey(string $key, Header &$header): string;
123
124
    /**
125
     * Decrypt a key.
126
     *
127
     * @param string $ciphertext Ciphertext of the encrypted key
128
     * @param Header $header     Header possibly containing encoding specific
129
     *                           parameters
130
     *
131
     * @return string Plaintext key
132
     */
133
    abstract protected function _decryptKey(string $ciphertext, Header $header): string;
134
}
135