Completed
Push — master ( 484ff8...780e48 )
by Jaime Pérez
15s queued 12s
created

AbstractKeyTransporter::getKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
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\RuntimeException;
11
use SimpleSAML\XMLSecurity\Key\AbstractKey;
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\Key\AbstractKey */
21
    private AbstractKey $key;
22
23
    /** @var \SimpleSAML\XMLSecurity\Backend\EncryptionBackend */
24
    protected EncryptionBackend $backend;
25
26
    /** @var string */
27
    protected string $default_backend;
28
29
    /** @var string */
30
    protected string $algId;
31
32
33
    /**
34
     * Build a key transport algorithm.
35
     *
36
     * Extend this class to implement your own key transporters.
37
     *
38
     * WARNING: remember to adjust the type of the key to the one that works with your algorithm!
39
     *
40
     * @param \SimpleSAML\XMLSecurity\Key\AbstractKey $key The encryption key.
41
     * @param string $algId The identifier of this algorithm.
42
     */
43
    public function __construct(AbstractKey $key, string $algId)
44
    {
45
        Assert::oneOf(
46
            $algId,
47
            static::getSupportedAlgorithms(),
48
            'Unsupported algorithm for ' . static::class,
49
            RuntimeException::class
50
        );
51
        $this->key = $key;
52
        $this->algId = $algId;
53
        $this->setBackend(new $this->default_backend());
54
    }
55
56
57
    /**
58
     * @return string
59
     */
60
    public function getAlgorithmId(): string
61
    {
62
        return $this->algId;
63
    }
64
65
66
    /**
67
     * @return AbstractKey
68
     */
69
    public function getKey(): AbstractKey
70
    {
71
        return $this->key;
72
    }
73
74
75
    /**
76
     * @inheritDoc
77
     */
78
    public function setBackend(?EncryptionBackend $backend): void
79
    {
80
        if ($backend === null) {
81
            return;
82
        }
83
84
        $this->backend = $backend;
85
        $this->backend->setCipher($this->algId);
86
    }
87
88
89
    /**
90
     * Encrypt a given key with this cipher and the loaded key.
91
     *
92
     * @param string $key The original key to encrypt.
93
     *
94
     * @return string The encrypted key (ciphertext).
95
     */
96
    public function encrypt(string $key): string
97
    {
98
        return $this->backend->encrypt($this->key, $key);
99
    }
100
101
102
    /**
103
     * Decrypt a given key with this cipher and the loaded key.
104
     *
105
     * @note The class of the returned key will depend on the algorithm it is going to be used for.
106
     *
107
     * @param string $key The encrypted key.
108
     *
109
     * @return string The decrypted key.
110
     */
111
    public function decrypt(string $key): string
112
    {
113
        return $this->backend->decrypt($this->key, $key);
114
    }
115
}
116