AESKW   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 66
c 0
b 0
f 0
wmc 8
lcom 1
cbo 2
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A wrapKey() 0 7 1
A unwrapKey() 0 7 1
A getKeyManagementMode() 0 4 1
A checkKey() 0 12 4
getKeySize() 0 1 ?
getWrapper() 0 1 ?
A allowedKeyTypes() 0 4 1
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\Algorithm\KeyEncryption;
15
16
use Base64Url\Base64Url;
17
use Jose\Component\Core\JWK;
18
19
/**
20
 * Class AESKW.
21
 */
22
abstract class AESKW implements KeyWrappingInterface
23
{
24
    /**
25
     * {@inheritdoc}
26
     */
27
    public function allowedKeyTypes(): array
28
    {
29
        return ['oct'];
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35
    public function wrapKey(JWK $key, string $cek, array $complete_headers, array &$additional_headers): string
36
    {
37
        $this->checkKey($key);
38
        $wrapper = $this->getWrapper();
39
40
        return $wrapper::wrap(Base64Url::decode($key->get('k')), $cek);
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46
    public function unwrapKey(JWK $key, string $encrypted_cek, array $complete_headers): string
47
    {
48
        $this->checkKey($key);
49
        $wrapper = $this->getWrapper();
50
51
        return $wrapper::unwrap(Base64Url::decode($key->get('k')), $encrypted_cek);
52
    }
53
54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function getKeyManagementMode(): string
58
    {
59
        return self::MODE_WRAP;
60
    }
61
62
    /**
63
     * @param JWK $key
64
     */
65
    protected function checkKey(JWK $key)
66
    {
67
        if ('oct' !== $key->get('kty')) {
68
            throw new \InvalidArgumentException('Wrong key type.');
69
        }
70
        if (!$key->has('k')) {
71
            throw new \InvalidArgumentException('The key parameter "k" is missing.');
72
        }
73
        if ($this->getKeySize() !== mb_strlen(Base64Url::decode($key->get('k')), '8bit')) {
74
            throw new \InvalidArgumentException('The key size is not valid');
75
        }
76
    }
77
78
    /**
79
     * @return int
80
     */
81
    abstract protected function getKeySize(): int;
82
83
    /**
84
     * @return \AESKW\A128KW|\AESKW\A192KW|\AESKW\A256KW
85
     */
86
    abstract protected function getWrapper();
87
}
88