Passed
Push — master ( a343df...d20b40 )
by Tim
10:40
created

AbstractKeyTransporter   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 21
c 2
b 0
f 0
dl 0
loc 96
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\XMLSecurity\Alg\KeyTransport;
6
7
use SimpleSAML\Assert\Assert;
8
use SimpleSAML\XMLSecurity\Alg\Encryption\EncryptionAlgorithmInterface;
9
use SimpleSAML\XMLSecurity\Backend\EncryptionBackend;
10
use SimpleSAML\XMLSecurity\Exception\UnsupportedAlgorithmException;
11
use SimpleSAML\XMLSecurity\Key\KeyInterface;
12
13
/**
14
 * An abstract class that implements a generic key transport algorithm.
15
 *
16
 * @package simplesamlphp/xml-security
17
 */
18
abstract class AbstractKeyTransporter implements EncryptionAlgorithmInterface
19
{
20
    /** @var \SimpleSAML\XMLSecurity\Backend\EncryptionBackend */
21
    protected EncryptionBackend $backend;
22
23
24
    /**
25
     * Build a key transport algorithm.
26
     *
27
     * Extend this class to implement your own key transporters.
28
     *
29
     * WARNING: remember to adjust the type of the key to the one that works with your algorithm!
30
     *
31
     * @param \SimpleSAML\XMLSecurity\Key\KeyInterface $key The encryption key.
32
     * @param string $algId The identifier of this algorithm.
33
     */
34
    public function __construct(
35
        private KeyInterface $key,
36
        protected string $algId,
37
    ) {
38
        Assert::oneOf(
39
            $algId,
40
            static::getSupportedAlgorithms(),
41
            'Unsupported algorithm for ' . static::class,
42
            UnsupportedAlgorithmException::class,
43
        );
44
        $this->setBackend(new (static::DEFAULT_BACKEND)());
0 ignored issues
show
Bug introduced by
A parse error occurred: Syntax error, unexpected '(', expecting ':' on line 44 at column 30
Loading history...
45
    }
46
47
48
    /**
49
     * @return string
50
     */
51
    public function getAlgorithmId(): string
52
    {
53
        return $this->algId;
54
    }
55
56
57
    /**
58
     * @return \SimpleSAML\XMLSecurity\Key\KeyInterface
59
     */
60
    public function getKey(): KeyInterface
61
    {
62
        return $this->key;
63
    }
64
65
66
    /**
67
     * @inheritDoc
68
     */
69
    public function setBackend(?EncryptionBackend $backend): void
70
    {
71
        if ($backend === null) {
72
            return;
73
        }
74
75
        $this->backend = $backend;
76
        $this->backend->setCipher($this->algId);
77
    }
78
79
80
    /**
81
     * Encrypt a given key with this cipher and the loaded key.
82
     *
83
     * @param string $plaintext The original key to encrypt.
84
     *
85
     * @return string The encrypted key (ciphertext).
86
     */
87
    public function encrypt(string $plaintext): string
88
    {
89
        return $this->backend->encrypt($this->key, $plaintext);
90
    }
91
92
93
    /**
94
     * Decrypt a given key with this cipher and the loaded key.
95
     *
96
     * @note The class of the returned key will depend on the algorithm it is going to be used for.
97
     *
98
     * @param string $ciphertext The encrypted key.
99
     *
100
     * @return string The decrypted key.
101
     */
102
    public function decrypt(string $ciphertext): string
103
    {
104
        return $this->backend->decrypt($this->key, $ciphertext);
105
    }
106
}
107