Failed Conditions
Push — master ( df44e5...b25e13 )
by Florent
02:17
created

JWEBuilder   F

Complexity

Total Complexity 71

Size/Duplication

Total Lines 552
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 15

Importance

Changes 0
Metric Value
wmc 71
lcom 1
cbo 15
dl 0
loc 552
rs 2.6315
c 0
b 0
f 0

28 Methods

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