AESCTR   A
last analyzed

Complexity

Total Complexity 13

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A allowedKeyTypes() 0 4 1
A encryptKey() 0 15 2
A decryptKey() 0 13 2
A getKeyManagementMode() 0 4 1
getMode() 0 1 ?
A getKey() 0 15 4
A checkHeaderAdditionalParameters() 0 9 3
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\Algorithm\KeyEncryption;
15
16
use Base64Url\Base64Url;
17
use InvalidArgumentException;
18
use Jose\Component\Core\JWK;
19
use RuntimeException;
20
21
abstract class AESCTR implements KeyEncryption
22
{
23
    public function allowedKeyTypes(): array
24
    {
25
        return ['oct'];
26
    }
27
28
    /**
29
     * @throws RuntimeException if the CEK cannot be encrypted
30
     */
31
    public function encryptKey(JWK $key, string $cek, array $completeHeader, array &$additionalHeader): string
32
    {
33
        $k = $this->getKey($key);
34
        $iv = random_bytes(16);
35
36
        // We set header parameters
37
        $additionalHeader['iv'] = Base64Url::encode($iv);
38
39
        $result = openssl_encrypt($cek, $this->getMode(), $k, OPENSSL_RAW_DATA, $iv);
40
        if (false === $result) {
41
            throw new RuntimeException('Unable to encrypt the CEK');
42
        }
43
44
        return $result;
45
    }
46
47
    /**
48
     * @throws RuntimeException if the CEK cannot be decrypted
49
     */
50
    public function decryptKey(JWK $key, string $encrypted_cek, array $header): string
51
    {
52
        $k = $this->getKey($key);
53
        $this->checkHeaderAdditionalParameters($header);
54
        $iv = Base64Url::decode($header['iv']);
55
56
        $result = openssl_decrypt($encrypted_cek, $this->getMode(), $k, OPENSSL_RAW_DATA, $iv);
57
        if (false === $result) {
58
            throw new RuntimeException('Unable to decrypt the CEK');
59
        }
60
61
        return $result;
62
    }
63
64
    public function getKeyManagementMode(): string
65
    {
66
        return self::MODE_ENCRYPT;
67
    }
68
69
    abstract protected function getMode(): string;
70
71
    /**
72
     * @throws InvalidArgumentException if the key is invalid
73
     */
74
    private function getKey(JWK $key): string
75
    {
76
        if (!\in_array($key->get('kty'), $this->allowedKeyTypes(), true)) {
77
            throw new InvalidArgumentException('Wrong key type.');
78
        }
79
        if (!$key->has('k')) {
80
            throw new InvalidArgumentException('The key parameter "k" is missing.');
81
        }
82
        $k = $key->get('k');
83
        if (!\is_string($k)) {
84
            throw new InvalidArgumentException('The key parameter "k" is invalid.');
85
        }
86
87
        return Base64Url::decode($k);
88
    }
89
90
    /**
91
     * @throws InvalidArgumentException if the IV is missing or invalid
92
     */
93
    private function checkHeaderAdditionalParameters(array $header): void
94
    {
95
        if (!isset($header['iv'])) {
96
            throw new InvalidArgumentException('The header parameter "iv" is missing.');
97
        }
98
        if (!\is_string($header['iv'])) {
99
            throw new InvalidArgumentException('The header parameter "iv" is not valid.');
100
        }
101
    }
102
}
103