JWEDecrypter::decryptRecipientKey()   A
last analyzed

Complexity

Conditions 5
Paths 15

Size

Total Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 35
rs 9.0488
c 0
b 0
f 0
cc 5
nc 15
nop 5
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2019 Spomky-Labs
9
 *
10
 * This software may be modified and distributed under the terms
11
 * of the MIT license.  See the LICENSE file for details.
12
 */
13
14
namespace Jose\Component\Encryption;
15
16
use InvalidArgumentException;
17
use Jose\Component\Core\Algorithm;
18
use Jose\Component\Core\AlgorithmManager;
19
use Jose\Component\Core\JWK;
20
use Jose\Component\Core\JWKSet;
21
use Jose\Component\Core\Util\KeyChecker;
22
use Jose\Component\Encryption\Algorithm\ContentEncryptionAlgorithm;
23
use Jose\Component\Encryption\Algorithm\KeyEncryption\DirectEncryption;
24
use Jose\Component\Encryption\Algorithm\KeyEncryption\KeyAgreement;
25
use Jose\Component\Encryption\Algorithm\KeyEncryption\KeyAgreementWithKeyWrapping;
26
use Jose\Component\Encryption\Algorithm\KeyEncryption\KeyEncryption;
27
use Jose\Component\Encryption\Algorithm\KeyEncryption\KeyWrapping;
28
use Jose\Component\Encryption\Algorithm\KeyEncryptionAlgorithm;
29
use Jose\Component\Encryption\Compression\CompressionMethodManager;
30
use Throwable;
31
32
class JWEDecrypter
33
{
34
    /**
35
     * @var AlgorithmManager
36
     */
37
    private $keyEncryptionAlgorithmManager;
38
39
    /**
40
     * @var AlgorithmManager
41
     */
42
    private $contentEncryptionAlgorithmManager;
43
44
    /**
45
     * @var CompressionMethodManager
46
     */
47
    private $compressionMethodManager;
48
49
    public function __construct(AlgorithmManager $keyEncryptionAlgorithmManager, AlgorithmManager $contentEncryptionAlgorithmManager, CompressionMethodManager $compressionMethodManager)
50
    {
51
        $this->keyEncryptionAlgorithmManager = $keyEncryptionAlgorithmManager;
52
        $this->contentEncryptionAlgorithmManager = $contentEncryptionAlgorithmManager;
53
        $this->compressionMethodManager = $compressionMethodManager;
54
    }
55
56
    /**
57
     * Returns the key encryption algorithm manager.
58
     */
59
    public function getKeyEncryptionAlgorithmManager(): AlgorithmManager
60
    {
61
        return $this->keyEncryptionAlgorithmManager;
62
    }
63
64
    /**
65
     * Returns the content encryption algorithm manager.
66
     */
67
    public function getContentEncryptionAlgorithmManager(): AlgorithmManager
68
    {
69
        return $this->contentEncryptionAlgorithmManager;
70
    }
71
72
    /**
73
     * Returns the compression method manager.
74
     */
75
    public function getCompressionMethodManager(): CompressionMethodManager
76
    {
77
        return $this->compressionMethodManager;
78
    }
79
80
    /**
81
     * This method will try to decrypt the given JWE and recipient using a JWK.
82
     *
83
     * @param JWE $jwe       A JWE object to decrypt
84
     * @param JWK $jwk       The key used to decrypt the input
85
     * @param int $recipient The recipient used to decrypt the token
86
     */
87
    public function decryptUsingKey(JWE &$jwe, JWK $jwk, int $recipient, ?JWK $senderKey = null): bool
88
    {
89
        $jwkset = new JWKSet([$jwk]);
90
91
        return $this->decryptUsingKeySet($jwe, $jwkset, $recipient, $senderKey);
92
    }
93
94
    /**
95
     * This method will try to decrypt the given JWE and recipient using a JWKSet.
96
     *
97
     * @param JWE    $jwe       A JWE object to decrypt
98
     * @param JWKSet $jwkset    The key set used to decrypt the input
99
     * @param JWK    $jwk       The key used to decrypt the token in case of success
100
     * @param int    $recipient The recipient used to decrypt the token in case of success
101
     *
102
     * @throws InvalidArgumentException if no key is set is the keyset
103
     * @throws InvalidArgumentException if the token has no recipients
104
     */
105
    public function decryptUsingKeySet(JWE &$jwe, JWKSet $jwkset, int $recipient, JWK &$jwk = null, ?JWK $senderKey = null): bool
106
    {
107
        if (0 === $jwkset->count()) {
108
            throw new InvalidArgumentException('No key in the key set.');
109
        }
110
        if (null !== $jwe->getPayload()) {
111
            return true;
112
        }
113
        if (0 === $jwe->countRecipients()) {
114
            throw new InvalidArgumentException('The JWE does not contain any recipient.');
115
        }
116
117
        $plaintext = $this->decryptRecipientKey($jwe, $jwkset, $recipient, $jwk, $senderKey);
118
        if (null !== $plaintext) {
119
            $jwe = $jwe->withPayload($plaintext);
120
121
            return true;
122
        }
123
124
        return false;
125
    }
126
127
    private function decryptRecipientKey(JWE $jwe, JWKSet $jwkset, int $i, JWK &$successJwk = null, ?JWK $senderKey = null): ?string
128
    {
129
        $recipient = $jwe->getRecipient($i);
130
        $completeHeader = array_merge($jwe->getSharedProtectedHeader(), $jwe->getSharedHeader(), $recipient->getHeader());
131
        $this->checkCompleteHeader($completeHeader);
132
133
        $key_encryption_algorithm = $this->getKeyEncryptionAlgorithm($completeHeader);
134
        $content_encryption_algorithm = $this->getContentEncryptionAlgorithm($completeHeader);
135
136
        $this->checkIvSize($jwe->getIV(), $content_encryption_algorithm->getIVSize());
137
138
        foreach ($jwkset as $recipientKey) {
139
            try {
140
                KeyChecker::checkKeyUsage($recipientKey, 'decryption');
141
                if ('dir' !== $key_encryption_algorithm->name()) {
142
                    KeyChecker::checkKeyAlgorithm($recipientKey, $key_encryption_algorithm->name());
143
                } else {
144
                    KeyChecker::checkKeyAlgorithm($recipientKey, $content_encryption_algorithm->name());
145
                }
146
                $cek = $this->decryptCEK($key_encryption_algorithm, $content_encryption_algorithm, $recipientKey, $senderKey, $recipient, $completeHeader);
147
                if (null !== $cek) {
148
                    $this->checkCekSize($cek, $key_encryption_algorithm, $content_encryption_algorithm);
149
                    $payload = $this->decryptPayload($jwe, $cek, $content_encryption_algorithm, $completeHeader);
150
                    $successJwk = $recipientKey;
151
152
                    return $payload;
153
                }
154
            } catch (Throwable $e) {
155
                //We do nothing, we continue with other keys
156
                continue;
157
            }
158
        }
159
160
        return null;
161
    }
162
163
    /**
164
     * @throws InvalidArgumentException if the Content Encryption Key size is invalid
165
     */
166
    private function checkCekSize(string $cek, KeyEncryptionAlgorithm $keyEncryptionAlgorithm, ContentEncryptionAlgorithm $algorithm): void
167
    {
168
        if ($keyEncryptionAlgorithm instanceof DirectEncryption || $keyEncryptionAlgorithm instanceof KeyAgreement) {
169
            return;
170
        }
171
        if (mb_strlen($cek, '8bit') * 8 !== $algorithm->getCEKSize()) {
172
            throw new InvalidArgumentException('Invalid CEK size');
173
        }
174
    }
175
176
    /**
177
     * @throws InvalidArgumentException if the IV size is invalid
178
     */
179
    private function checkIvSize(?string $iv, int $requiredIvSize): void
180
    {
181
        if (null === $iv && 0 !== $requiredIvSize) {
182
            throw new InvalidArgumentException('Invalid IV size');
183
        }
184
        if (\is_string($iv) && mb_strlen($iv, '8bit') !== $requiredIvSize / 8) {
185
            throw new InvalidArgumentException('Invalid IV size');
186
        }
187
    }
188
189
    /**
190
     * @throws InvalidArgumentException if the CEK creation method is not supported
191
     */
192
    private function decryptCEK(Algorithm $key_encryption_algorithm, ContentEncryptionAlgorithm $content_encryption_algorithm, JWK $recipientKey, ?JWK $senderKey, Recipient $recipient, array $completeHeader): ?string
193
    {
194
        if ($key_encryption_algorithm instanceof DirectEncryption) {
195
            return $key_encryption_algorithm->getCEK($recipientKey);
196
        }
197
        if ($key_encryption_algorithm instanceof KeyAgreement) {
198
            return $key_encryption_algorithm->getAgreementKey($content_encryption_algorithm->getCEKSize(), $content_encryption_algorithm->name(), $recipientKey, $senderKey, $completeHeader);
199
        }
200
        if ($key_encryption_algorithm instanceof KeyAgreementWithKeyWrapping) {
201
            return $key_encryption_algorithm->unwrapAgreementKey($recipientKey, $senderKey, $recipient->getEncryptedKey(), $content_encryption_algorithm->getCEKSize(), $completeHeader);
202
        }
203
        if ($key_encryption_algorithm instanceof KeyEncryption) {
204
            return $key_encryption_algorithm->decryptKey($recipientKey, $recipient->getEncryptedKey(), $completeHeader);
205
        }
206
        if ($key_encryption_algorithm instanceof KeyWrapping) {
207
            return $key_encryption_algorithm->unwrapKey($recipientKey, $recipient->getEncryptedKey(), $completeHeader);
208
        }
209
210
        throw new InvalidArgumentException('Unsupported CEK generation');
211
    }
212
213
    private function decryptPayload(JWE $jwe, string $cek, ContentEncryptionAlgorithm $content_encryption_algorithm, array $completeHeader): string
214
    {
215
        $payload = $content_encryption_algorithm->decryptContent($jwe->getCiphertext(), $cek, $jwe->getIV(), $jwe->getAAD(), $jwe->getEncodedSharedProtectedHeader(), $jwe->getTag());
216
217
        return $this->decompressIfNeeded($payload, $completeHeader);
218
    }
219
220
    private function decompressIfNeeded(string $payload, array $completeHeaders): string
221
    {
222
        if (\array_key_exists('zip', $completeHeaders)) {
223
            $compression_method = $this->compressionMethodManager->get($completeHeaders['zip']);
224
            $payload = $compression_method->uncompress($payload);
225
        }
226
227
        return $payload;
228
    }
229
230
    /**
231
     * @throws InvalidArgumentException if a header parameter is missing
232
     */
233
    private function checkCompleteHeader(array $completeHeaders): void
234
    {
235
        foreach (['enc', 'alg'] as $key) {
236
            if (!isset($completeHeaders[$key])) {
237
                throw new InvalidArgumentException(sprintf("Parameter '%s' is missing.", $key));
238
            }
239
        }
240
    }
241
242
    /**
243
     * @throws InvalidArgumentException if the key encryption algorithm is not supported or does not implement the KeyEncryptionAlgorithm interface
244
     */
245
    private function getKeyEncryptionAlgorithm(array $completeHeaders): KeyEncryptionAlgorithm
246
    {
247
        $key_encryption_algorithm = $this->keyEncryptionAlgorithmManager->get($completeHeaders['alg']);
248
        if (!$key_encryption_algorithm instanceof KeyEncryptionAlgorithm) {
249
            throw new InvalidArgumentException(sprintf('The key encryption algorithm "%s" is not supported or does not implement KeyEncryptionAlgorithm interface.', $completeHeaders['alg']));
250
        }
251
252
        return $key_encryption_algorithm;
253
    }
254
255
    /**
256
     * @throws InvalidArgumentException if the content encryption algorithm is not supported or does not implement the ContentEncryption interface
257
     */
258
    private function getContentEncryptionAlgorithm(array $completeHeader): ContentEncryptionAlgorithm
259
    {
260
        $content_encryption_algorithm = $this->contentEncryptionAlgorithmManager->get($completeHeader['enc']);
261
        if (!$content_encryption_algorithm instanceof ContentEncryptionAlgorithm) {
262
            throw new InvalidArgumentException(sprintf('The key encryption algorithm "%s" is not supported or does not implement the ContentEncryption interface.', $completeHeader['enc']));
263
        }
264
265
        return $content_encryption_algorithm;
266
    }
267
}
268