Failed Conditions
Pull Request — v7 (#248)
by Florent
05:23 queued 02:25
created

JWEBuilder   F

Complexity

Total Complexity 72

Size/Duplication

Total Lines 535
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 0
Metric Value
wmc 72
lcom 1
cbo 15
dl 0
loc 535
rs 2.5423
c 0
b 0
f 0

27 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getSupportedKeyEncryptionAlgorithms() 0 4 1
A getSupportedContentEncryptionAlgorithms() 0 4 1
A getSupportedCompressionMethods() 0 4 1
A withPayload() 0 11 3
A withAAD() 0 7 1
A withSharedProtectedHeaders() 0 11 2
A withSharedHeaders() 0 11 2
C addRecipient() 0 36 8
B build() 0 26 6
A checkAndSetContentEncryptionAlgorithm() 0 9 3
A processRecipient() 0 14 3
A encryptJWE() 0 11 2
B preparePayload() 0 17 5
B getEncryptedKey() 0 16 6
A getEncryptedKeyFromKeyAgreementAndKeyWrappingAlgorithm() 0 4 1
A getEncryptedKeyFromKeyEncryptionAlgorithm() 0 4 1
A getEncryptedKeyFromKeyWrappingAlgorithm() 0 4 1
A checkKey() 0 9 2
C determineCEK() 0 32 8
A getCompressionMethod() 0 8 2
A areKeyManagementModesCompatible() 0 14 2
A createCEK() 0 4 1
A createIV() 0 4 1
A getKeyEncryptionAlgorithm() 0 12 3
A getContentEncryptionAlgorithm() 0 12 3
A checkDuplicatedHeaderParameters() 0 7 2

How to fix   Complexity   

Complex Class

Complex classes like JWEBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use JWEBuilder, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * The MIT License (MIT)
7
 *
8
 * Copyright (c) 2014-2017 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 Base64Url\Base64Url;
17
use Jose\Component\Core\Encoder\PayloadEncoderInterface;
18
use Jose\Component\Core\JWAManager;
19
use Jose\Component\Core\JWK;
20
use Jose\Component\Core\Util\KeyChecker;
21
use Jose\Component\Encryption\Algorithm\ContentEncryptionAlgorithmInterface;
22
use Jose\Component\Encryption\Algorithm\KeyEncryption\DirectEncryptionInterface;
23
use Jose\Component\Encryption\Algorithm\KeyEncryption\KeyAgreementInterface;
24
use Jose\Component\Encryption\Algorithm\KeyEncryption\KeyAgreementWrappingInterface;
25
use Jose\Component\Encryption\Algorithm\KeyEncryption\KeyEncryptionInterface;
26
use Jose\Component\Encryption\Algorithm\KeyEncryption\KeyWrappingInterface;
27
use Jose\Component\Encryption\Algorithm\KeyEncryptionAlgorithmInterface;
28
use Jose\Component\Encryption\Compression\CompressionMethodInterface;
29
use Jose\Component\Encryption\Compression\CompressionMethodManager;
30
31
final class JWEBuilder
32
{
33
    /**
34
     * @var PayloadEncoderInterface
35
     */
36
    private $payloadEncoder;
37
    /**
38
     * @var mixed
39
     */
40
    private $payload;
41
42
    /**
43
     * @var string|null
44
     */
45
    private $aad;
46
47
    /**
48
     * @var array
49
     */
50
    private $recipients = [];
51
52
    /**
53
     * @var JWAManager
54
     */
55
    private $keyEncryptionAlgorithmManager;
56
57
    /**
58
     * @var JWAManager
59
     */
60
    private $contentEncryptionAlgorithmManager;
61
62
    /**
63
     * @var CompressionMethodManager
64
     */
65
    private $compressionManager;
66
67
    /**
68
     * @var array
69
     */
70
    private $sharedProtectedHeaders = [];
71
72
    /**
73
     * @var array
74
     */
75
    private $sharedHeaders = [];
76
77
    /**
78
     * @var null|CompressionMethodInterface
79
     */
80
    private $compressionMethod = null;
81
82
    /**
83
     * @var null|ContentEncryptionAlgorithmInterface
84
     */
85
    private $contentEncryptionAlgorithm = null;
86
87
    /**
88
     * @var null|string
89
     */
90
    private $keyManagementMode = null;
91
92
    /**
93
     * JWEBuilder constructor.
94
     *
95
     * @param PayloadEncoderInterface  $payloadEncoder
96
     * @param JWAManager               $keyEncryptionAlgorithmManager
97
     * @param JWAManager               $contentEncryptionAlgorithmManager
98
     * @param CompressionMethodManager $compressionManager
99
     */
100
    public function __construct(PayloadEncoderInterface $payloadEncoder, JWAManager $keyEncryptionAlgorithmManager, JWAManager $contentEncryptionAlgorithmManager, CompressionMethodManager $compressionManager)
101
    {
102
        $this->payloadEncoder = $payloadEncoder;
103
        $this->keyEncryptionAlgorithmManager = $keyEncryptionAlgorithmManager;
104
        $this->contentEncryptionAlgorithmManager = $contentEncryptionAlgorithmManager;
105
        $this->compressionManager = $compressionManager;
106
    }
107
108
    /**
109
     * @return string[]
110
     */
111
    public function getSupportedKeyEncryptionAlgorithms(): array
112
    {
113
        return $this->keyEncryptionAlgorithmManager->list();
114
    }
115
116
    /**
117
     * @return string[]
118
     */
119
    public function getSupportedContentEncryptionAlgorithms(): array
120
    {
121
        return $this->contentEncryptionAlgorithmManager->list();
122
    }
123
124
    /**
125
     * @return string[]
126
     */
127
    public function getSupportedCompressionMethods(): array
128
    {
129
        return $this->compressionManager->list();
130
    }
131
132
    /**
133
     * @param mixed $payload
134
     *
135
     * @return JWEBuilder
136
     */
137
    public function withPayload($payload): JWEBuilder
138
    {
139
        $payload = is_string($payload) ? $payload : $this->payloadEncoder->encode($payload);
140
        if (false === mb_detect_encoding($payload, 'UTF-8', true)) {
141
            throw new \InvalidArgumentException('The payload must be encoded in UTF-8');
142
        }
143
        $clone = clone $this;
144
        $clone->payload = $payload;
145
146
        return $clone;
147
    }
148
149
    /**
150
     * @param string|null $aad
151
     *
152
     * @return JWEBuilder
153
     */
154
    public function withAAD(?string $aad): JWEBuilder
155
    {
156
        $clone = clone $this;
157
        $clone->aad = $aad;
158
159
        return $clone;
160
    }
161
162
    /**
163
     * @param array $sharedProtectedHeaders
164
     *
165
     * @return JWEBuilder
166
     */
167
    public function withSharedProtectedHeaders(array $sharedProtectedHeaders): JWEBuilder
168
    {
169
        $this->checkDuplicatedHeaderParameters($sharedProtectedHeaders, $this->sharedHeaders);
170
        foreach ($this->recipients as $recipient) {
171
            $this->checkDuplicatedHeaderParameters($sharedProtectedHeaders, $recipient->getHeaders());
172
        }
173
        $clone = clone $this;
174
        $clone->sharedProtectedHeaders = $sharedProtectedHeaders;
175
176
        return $clone;
177
    }
178
179
    /**
180
     * @param array $sharedHeaders
181
     *
182
     * @return JWEBuilder
183
     */
184
    public function withSharedHeaders(array $sharedHeaders): JWEBuilder
185
    {
186
        $this->checkDuplicatedHeaderParameters($this->sharedProtectedHeaders, $sharedHeaders);
187
        foreach ($this->recipients as $recipient) {
188
            $this->checkDuplicatedHeaderParameters($sharedHeaders, $recipient->getHeaders());
189
        }
190
        $clone = clone $this;
191
        $clone->sharedHeaders = $sharedHeaders;
192
193
        return $clone;
194
    }
195
196
    /**
197
     * @param JWK   $recipientKey
198
     * @param array $recipientHeaders
199
     *
200
     * @return JWEBuilder
201
     */
202
    public function addRecipient(JWK $recipientKey, array $recipientHeaders = []): JWEBuilder
203
    {
204
        $this->checkDuplicatedHeaderParameters($this->sharedProtectedHeaders, $recipientHeaders);
205
        $this->checkDuplicatedHeaderParameters($this->sharedHeaders, $recipientHeaders);
206
        $clone = clone $this;
207
        $completeHeaders = array_merge($clone->sharedHeaders, $recipientHeaders, $clone->sharedProtectedHeaders);
208
        $clone->checkAndSetContentEncryptionAlgorithm($completeHeaders);
209
        $keyEncryptionAlgorithm = $clone->getKeyEncryptionAlgorithm($completeHeaders);
210
        if (null === $clone->keyManagementMode) {
211
            $clone->keyManagementMode = $keyEncryptionAlgorithm->getKeyManagementMode();
212
        } else {
213
            if (!$clone->areKeyManagementModesCompatible($clone->keyManagementMode, $keyEncryptionAlgorithm->getKeyManagementMode())) {
214
                throw new \InvalidArgumentException('Foreign key management mode forbidden.');
215
            }
216
        }
217
218
        $compressionMethod = $clone->getCompressionMethod($completeHeaders);
219
        if (null !== $compressionMethod) {
220
            if (null === $clone->compressionMethod) {
221
                $clone->compressionMethod = $compressionMethod;
222
            } elseif ($clone->compressionMethod->name() !== $compressionMethod->name()) {
223
                throw new \InvalidArgumentException('Incompatible compression method.');
224
            }
225
        }
226
        if (null === $compressionMethod && null !== $clone->compressionMethod) {
227
            throw new \InvalidArgumentException('Inconsistent compression method.');
228
        }
229
        $clone->checkKey($keyEncryptionAlgorithm, $recipientKey);
230
        $clone->recipients[] = [
231
            'key' => $recipientKey,
232
            'headers' => $recipientHeaders,
233
            'key_encryption_algorithm' => $keyEncryptionAlgorithm,
234
        ];
235
236
        return $clone;
237
    }
238
239
    /**
240
     * @return JWE
241
     */
242
    public function build(): JWE
243
    {
244
        if (0 === count($this->recipients)) {
245
            throw new \LogicException('No recipient.');
246
        }
247
248
        $additionalHeaders = [];
249
        $cek = $this->determineCEK($additionalHeaders);
250
251
        $recipients = [];
252
        foreach ($this->recipients as $recipient) {
253
            $recipient = $this->processRecipient($recipient, $cek, $additionalHeaders);
254
            $recipients[] = $recipient;
255
        }
256
257
        if (!empty($additionalHeaders) && 1 === count($this->recipients)) {
258
            $sharedProtectedHeaders = array_merge($additionalHeaders, $this->sharedProtectedHeaders);
259
        } else {
260
            $sharedProtectedHeaders = $this->sharedProtectedHeaders;
261
        }
262
        $encodedSharedProtectedHeaders = empty($sharedProtectedHeaders) ? '' : Base64Url::encode(json_encode($sharedProtectedHeaders));
263
264
        list($ciphertext, $iv, $tag) = $this->encryptJWE($cek, $encodedSharedProtectedHeaders);
265
266
        return JWE::create($ciphertext, $iv, $this->aad, $tag, $this->sharedHeaders, $sharedProtectedHeaders, $encodedSharedProtectedHeaders, $recipients);
267
    }
268
269
    /**
270
     * @param array $completeHeaders
271
     */
272
    private function checkAndSetContentEncryptionAlgorithm(array $completeHeaders): void
273
    {
274
        $contentEncryptionAlgorithm = $this->getContentEncryptionAlgorithm($completeHeaders);
275
        if (null === $this->contentEncryptionAlgorithm) {
276
            $this->contentEncryptionAlgorithm = $contentEncryptionAlgorithm;
277
        } elseif ($contentEncryptionAlgorithm->name() !== $this->contentEncryptionAlgorithm->name()) {
278
            throw new \InvalidArgumentException('Inconsistent content encryption algorithm');
279
        }
280
    }
281
282
    /**
283
     * @param array  $recipient
284
     * @param string $cek
285
     * @param array  $additionalHeaders
286
     *
287
     * @return Recipient
288
     */
289
    private function processRecipient(array $recipient, string $cek, array &$additionalHeaders): Recipient
290
    {
291
        $completeHeaders = array_merge($this->sharedHeaders, $recipient['headers'], $this->sharedProtectedHeaders);
292
        /** @var KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm */
293
        $keyEncryptionAlgorithm = $recipient['key_encryption_algorithm'];
294
        $encryptedContentEncryptionKey = $this->getEncryptedKey($completeHeaders, $cek, $keyEncryptionAlgorithm, $additionalHeaders, $recipient['key']);
295
        $recipientHeaders = $recipient['headers'];
296
        if (!empty($additionalHeaders) && 1 !== count($this->recipients)) {
297
            $recipientHeaders = array_merge($recipientHeaders, $additionalHeaders);
298
            $additionalHeaders = [];
299
        }
300
301
        return Recipient::create($recipientHeaders, $encryptedContentEncryptionKey);
302
    }
303
304
    /**
305
     * @param string $cek
306
     * @param string $encodedSharedProtectedHeaders
307
     *
308
     * @return array
309
     */
310
    private function encryptJWE(string $cek, string $encodedSharedProtectedHeaders): array
311
    {
312
        $tag = null;
313
        $iv_size = $this->contentEncryptionAlgorithm->getIVSize();
314
        $iv = $this->createIV($iv_size);
315
        $payload = $this->preparePayload();
316
        $aad = $this->aad ? Base64Url::encode($this->aad) : null;
317
        $ciphertext = $this->contentEncryptionAlgorithm->encryptContent($payload, $cek, $iv, $aad, $encodedSharedProtectedHeaders, $tag);
318
319
        return [$ciphertext, $iv, $tag];
320
    }
321
322
    /**
323
     * @return string
324
     */
325
    private function preparePayload(): ?string
326
    {
327
        $prepared = is_string($this->payload) ? $this->payload : json_encode($this->payload);
328
        if (null === $prepared) {
329
            throw new \RuntimeException('The payload is empty or cannot encoded into JSON.');
330
        }
331
332
        if (null === $this->compressionMethod) {
333
            return $prepared;
334
        }
335
        $compressedPayload = $this->compressionMethod->compress($prepared);
336
        if (null === $compressedPayload) {
337
            throw new \RuntimeException('The payload cannot be compressed.');
338
        }
339
340
        return $compressedPayload;
341
    }
342
343
    /**
344
     * @param array                           $completeHeaders
345
     * @param string                          $cek
346
     * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm
347
     * @param JWK                             $recipientKey
348
     * @param array                           $additionalHeaders
349
     *
350
     * @return string|null
351
     */
352
    private function getEncryptedKey(array $completeHeaders, string $cek, KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): ?string
353
    {
354
        if ($keyEncryptionAlgorithm instanceof KeyEncryptionInterface) {
355
            return $this->getEncryptedKeyFromKeyEncryptionAlgorithm($completeHeaders, $cek, $keyEncryptionAlgorithm, $recipientKey, $additionalHeaders);
356
        } elseif ($keyEncryptionAlgorithm instanceof KeyWrappingInterface) {
357
            return $this->getEncryptedKeyFromKeyWrappingAlgorithm($completeHeaders, $cek, $keyEncryptionAlgorithm, $recipientKey, $additionalHeaders);
358
        } elseif ($keyEncryptionAlgorithm instanceof KeyAgreementWrappingInterface) {
359
            return $this->getEncryptedKeyFromKeyAgreementAndKeyWrappingAlgorithm($completeHeaders, $cek, $keyEncryptionAlgorithm, $additionalHeaders, $recipientKey);
360
        } elseif ($keyEncryptionAlgorithm instanceof KeyAgreementInterface) {
361
            return null;
362
        } elseif ($keyEncryptionAlgorithm instanceof DirectEncryptionInterface) {
363
            return null;
364
        }
365
366
        throw new \InvalidArgumentException('Unsupported key encryption algorithm.');
367
    }
368
369
    /**
370
     * @param array                         $completeHeaders
371
     * @param string                        $cek
372
     * @param KeyAgreementWrappingInterface $keyEncryptionAlgorithm
373
     * @param array                         $additionalHeaders
374
     * @param JWK                           $recipientKey
375
     *
376
     * @return string
377
     */
378
    private function getEncryptedKeyFromKeyAgreementAndKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyAgreementWrappingInterface $keyEncryptionAlgorithm, array &$additionalHeaders, JWK $recipientKey): string
379
    {
380
        return $keyEncryptionAlgorithm->wrapAgreementKey($recipientKey, $cek, $this->contentEncryptionAlgorithm->getCEKSize(), $completeHeaders, $additionalHeaders);
381
    }
382
383
    /**
384
     * @param array                  $completeHeaders
385
     * @param string                 $cek
386
     * @param KeyEncryptionInterface $keyEncryptionAlgorithm
387
     * @param JWK                    $recipientKey
388
     * @param array                  $additionalHeaders
389
     *
390
     * @return string
391
     */
392
    private function getEncryptedKeyFromKeyEncryptionAlgorithm(array $completeHeaders, string $cek, KeyEncryptionInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string
393
    {
394
        return $keyEncryptionAlgorithm->encryptKey($recipientKey, $cek, $completeHeaders, $additionalHeaders);
395
    }
396
397
    /**
398
     * @param array                $completeHeaders
399
     * @param string               $cek
400
     * @param KeyWrappingInterface $keyEncryptionAlgorithm
401
     * @param JWK                  $recipientKey
402
     * @param array                $additionalHeaders
403
     *
404
     * @return string
405
     */
406
    private function getEncryptedKeyFromKeyWrappingAlgorithm(array $completeHeaders, string $cek, KeyWrappingInterface $keyEncryptionAlgorithm, JWK $recipientKey, array &$additionalHeaders): string
407
    {
408
        return $keyEncryptionAlgorithm->wrapKey($recipientKey, $cek, $completeHeaders, $additionalHeaders);
409
    }
410
411
    /**
412
     * @param KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm
413
     * @param JWK                             $recipientKey
414
     */
415
    private function checkKey(KeyEncryptionAlgorithmInterface $keyEncryptionAlgorithm, JWK $recipientKey)
416
    {
417
        KeyChecker::checkKeyUsage($recipientKey, 'encryption');
418
        if ('dir' !== $keyEncryptionAlgorithm->name()) {
419
            KeyChecker::checkKeyAlgorithm($recipientKey, $keyEncryptionAlgorithm->name());
420
        } else {
421
            KeyChecker::checkKeyAlgorithm($recipientKey, $this->contentEncryptionAlgorithm->name());
422
        }
423
    }
424
425
    /**
426
     * @param array $additionalHeaders
427
     *
428
     * @return string
429
     */
430
    private function determineCEK(array &$additionalHeaders): string
431
    {
432
        switch ($this->keyManagementMode) {
433
            case KeyEncryptionInterface::MODE_ENCRYPT:
434
            case KeyEncryptionInterface::MODE_WRAP:
435
                return $this->createCEK($this->contentEncryptionAlgorithm->getCEKSize());
436
            case KeyEncryptionInterface::MODE_AGREEMENT:
437
                if (1 !== count($this->recipients)) {
438
                    throw new \LogicException('Unable to encrypt for multiple recipients using key agreement algorithms.');
439
                }
440
                /** @var JWK $key */
441
                $key = $this->recipients[0]['key'];
442
                /** @var KeyAgreementInterface $algorithm */
443
                $algorithm = $this->recipients[0]['key_encryption_algorithm'];
444
                $completeHeaders = array_merge($this->sharedHeaders, $this->recipients[0]['headers'], $this->sharedProtectedHeaders);
445
446
                return $algorithm->getAgreementKey($this->contentEncryptionAlgorithm->getCEKSize(), $this->contentEncryptionAlgorithm->name(), $key, $completeHeaders, $additionalHeaders);
447
            case KeyEncryptionInterface::MODE_DIRECT:
448
                if (1 !== count($this->recipients)) {
449
                    throw new \LogicException('Unable to encrypt for multiple recipients using key agreement algorithms.');
450
                }
451
                /** @var JWK $key */
452
                $key = $this->recipients[0]['key'];
453
                if ('oct' !== $key->get('kty')) {
454
                    throw new \RuntimeException('Wrong key type.');
455
                }
456
457
                return Base64Url::decode($key->get('k'));
458
            default:
459
                throw new \InvalidArgumentException(sprintf('Unsupported key management mode "%s".', $this->keyManagementMode));
460
        }
461
    }
462
463
    /**
464
     * @param array $completeHeaders
465
     *
466
     * @return CompressionMethodInterface|null
467
     */
468
    private function getCompressionMethod(array $completeHeaders): ?CompressionMethodInterface
469
    {
470
        if (!array_key_exists('zip', $completeHeaders)) {
471
            return null;
472
        }
473
474
        return $this->compressionManager->get($completeHeaders['zip']);
475
    }
476
477
    /**
478
     * @param string $current
479
     * @param string $new
480
     *
481
     * @return bool
482
     */
483
    private function areKeyManagementModesCompatible(string $current, string $new): bool
484
    {
485
        $agree = KeyEncryptionAlgorithmInterface::MODE_AGREEMENT;
486
        $dir = KeyEncryptionAlgorithmInterface::MODE_DIRECT;
487
        $enc = KeyEncryptionAlgorithmInterface::MODE_ENCRYPT;
488
        $wrap = KeyEncryptionAlgorithmInterface::MODE_WRAP;
489
        $supportedKeyManagementModeCombinations = [$enc.$enc => true, $enc.$wrap => true, $wrap.$enc => true, $wrap.$wrap => true, $agree.$agree => false, $agree.$dir => false, $agree.$enc => false, $agree.$wrap => false, $dir.$agree => false, $dir.$dir => false, $dir.$enc => false, $dir.$wrap => false, $enc.$agree => false, $enc.$dir => false, $wrap.$agree => false, $wrap.$dir => false];
490
491
        if (array_key_exists($current.$new, $supportedKeyManagementModeCombinations)) {
492
            return $supportedKeyManagementModeCombinations[$current.$new];
493
        }
494
495
        return false;
496
    }
497
498
    /**
499
     * @param int $size
500
     *
501
     * @return string
502
     */
503
    private function createCEK(int $size): string
504
    {
505
        return random_bytes($size / 8);
506
    }
507
508
    /**
509
     * @param int $size
510
     *
511
     * @return string
512
     */
513
    private function createIV(int $size): string
514
    {
515
        return random_bytes($size / 8);
516
    }
517
518
    /**
519
     * @param array $completeHeaders
520
     *
521
     * @return KeyEncryptionAlgorithmInterface
522
     */
523
    private function getKeyEncryptionAlgorithm(array $completeHeaders): KeyEncryptionAlgorithmInterface
524
    {
525
        if (!array_key_exists('alg', $completeHeaders)) {
526
            throw new \InvalidArgumentException('Parameter "alg" is missing.');
527
        }
528
        $keyEncryptionAlgorithm = $this->keyEncryptionAlgorithmManager->get($completeHeaders['alg']);
529
        if (!$keyEncryptionAlgorithm instanceof KeyEncryptionAlgorithmInterface) {
530
            throw new \InvalidArgumentException(sprintf('The key encryption algorithm "%s" is not supported or not a key encryption algorithm instance.', $completeHeaders['alg']));
531
        }
532
533
        return $keyEncryptionAlgorithm;
534
    }
535
536
    /**
537
     * @param array $completeHeaders
538
     *
539
     * @return ContentEncryptionAlgorithmInterface
540
     */
541
    private function getContentEncryptionAlgorithm(array $completeHeaders): ContentEncryptionAlgorithmInterface
542
    {
543
        if (!array_key_exists('enc', $completeHeaders)) {
544
            throw new \InvalidArgumentException('Parameter "enc" is missing.');
545
        }
546
        $contentEncryptionAlgorithm = $this->contentEncryptionAlgorithmManager->get($completeHeaders['enc']);
547
        if (!$contentEncryptionAlgorithm instanceof ContentEncryptionAlgorithmInterface) {
548
            throw new \InvalidArgumentException(sprintf('The content encryption algorithm "%s" is not supported or not a content encryption algorithm instance.', $completeHeaders['alg']));
549
        }
550
551
        return $contentEncryptionAlgorithm;
552
    }
553
554
    /**
555
     * @param array $header1
556
     * @param array $header2
557
     */
558
    private function checkDuplicatedHeaderParameters(array $header1, array $header2)
559
    {
560
        $inter = array_intersect_key($header1, $header2);
561
        if (!empty($inter)) {
562
            throw new \InvalidArgumentException(sprintf('The header contains duplicated entries: %s.', json_encode(array_keys($inter))));
563
        }
564
    }
565
}
566