Passed
Push — master ( 41d929...1c0b0d )
by Tim
03:27
created

KeyTransportAlgorithmFactory::getExpectedParent()   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\Constants as C;
9
use SimpleSAML\XMLSecurity\Exception\BlacklistedAlgorithmException;
10
use SimpleSAML\XMLSecurity\Exception\UnsupportedAlgorithmException;
11
use SimpleSAML\XMLSecurity\Key\KeyInterface;
12
13
use function array_key_exists;
14
use function in_array;
15
use function sprintf;
16
17
/**
18
 * Factory class to create and configure key transport algorithms.
19
 */
20
class KeyTransportAlgorithmFactory
21
{
22
    /**
23
     * A cache of algorithm implementations indexed by algorithm ID.
24
     *
25
     * @var array<string, \SimpleSAML\XMLSecurity\Alg\KeyTransport\KeyTransportAlgorithmInterface>
26
     */
27
    protected static array $cache = [];
28
29
    /**
30
     * Whether the factory has been initialized or not.
31
     *
32
     * @var bool
33
     */
34
    protected static bool $initialized = false;
35
36
    /**
37
     * An array of blacklisted algorithms.
38
     *
39
     * Defaults to RSA 1.5.
40
     *
41
     * @var string[]
42
     */
43
    private const DEFAULT_BLACKLIST = [
44
        C::KEY_TRANSPORT_RSA_1_5,
45
    ];
46
47
48
    /**
49
     * An array of default algorithms that can be used.
50
     *
51
     * @var class-string[]
52
     */
53
    private const SUPPORTED_DEFAULTS = [
54
        RSA::class,
55
    ];
56
57
58
    /**
59
     * Build a factory that creates algorithms.
60
     *
61
     * @param string[]|null $blacklist A list of algorithms forbidden for their use.
62
     */
63
    public function __construct(
64
        protected ?array $blacklist = self::DEFAULT_BLACKLIST,
65
    ) {
66
        // initialize the cache for supported algorithms per known implementation
67
        if (!self::$initialized && $blacklist !== null) {
68
            foreach (self::SUPPORTED_DEFAULTS as $algorithm) {
69
                foreach ($algorithm::getSupportedAlgorithms() as $algId) {
70
                    if (array_key_exists($algId, self::$cache)) {
71
                        /*
72
                         * If the key existed before initialization, that means someone registered a handler for this
73
                         * algorithm, so we should respect that and skip registering the default here.
74
                         */
75
                        continue;
76
                    }
77
                    self::$cache[$algId] = $algorithm;
78
                }
79
            }
80
            self::$initialized = true;
81
        }
82
    }
83
84
85
    /**
86
     * Get a new object implementing the given digital signature algorithm.
87
     *
88
     * @param string $algId The identifier of the algorithm desired.
89
     * @param \SimpleSAML\XMLSecurity\Key\KeyInterface $key The key to use with the given algorithm.
90
     *
91
     * @return \SimpleSAML\XMLSecurity\Alg\KeyTransport\KeyTransportAlgorithmInterface An object implementing the given
92
     * algorithm.
93
     *
94
     * @throws \SimpleSAML\XMLSecurity\Exception\UnsupportedAlgorithmException If an error occurs, e.g. the given
95
     * algorithm is blacklisted, unknown or the given key is not suitable for it.
96
     */
97
    public function getAlgorithm(string $algId, KeyInterface $key): KeyTransportAlgorithmInterface
98
    {
99
        Assert::false(
100
            ($this->blacklist !== null) && in_array($algId, $this->blacklist, true),
101
            sprintf('Blacklisted algorithm: \'%s\'.', $algId),
102
            BlacklistedAlgorithmException::class,
103
        );
104
        Assert::keyExists(
105
            self::$cache,
106
            $algId,
107
            sprintf('Unknown or unsupported algorithm: \'%s\'.', $algId),
108
            UnsupportedAlgorithmException::class,
109
        );
110
111
        return new self::$cache[$algId]($key, $algId);
112
    }
113
114
115
    /**
116
     * Register an implementation of some algorithm(s) for its use.
117
     *
118
     * @param class-string $className
1 ignored issue
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
119
     */
120
    public static function registerAlgorithm(string $className): void
121
    {
122
        Assert::implementsInterface(
123
            $className,
124
            KeyTransportAlgorithmInterface::class,
125
            sprintf(
126
                'Cannot register algorithm "%s", must implement %s.',
127
                $className,
128
                KeyTransportAlgorithmInterface::class,
129
            ),
130
        );
131
132
        foreach ($className::getSupportedAlgorithms() as $algId) {
133
            self::$cache[$algId] = $className;
134
        }
135
    }
136
}
137