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\Bundle\JoseFramework\DependencyInjection\Source\Encryption; |
15
|
|
|
|
16
|
|
|
use Jose\Bundle\JoseFramework\DependencyInjection\Compiler; |
17
|
|
|
use Jose\Bundle\JoseFramework\DependencyInjection\Source\Source; |
18
|
|
|
use Jose\Bundle\JoseFramework\DependencyInjection\Source\SourceWithCompilerPasses; |
19
|
|
|
use Jose\Component\Encryption\Algorithm\ContentEncryption\AESCBCHS; |
20
|
|
|
use Jose\Component\Encryption\Algorithm\ContentEncryption\AESGCM; |
21
|
|
|
use Jose\Component\Encryption\Algorithm\KeyEncryption\A128CTR; |
22
|
|
|
use Jose\Component\Encryption\Algorithm\KeyEncryption\AESGCMKW; |
23
|
|
|
use Jose\Component\Encryption\Algorithm\KeyEncryption\AESKW; |
24
|
|
|
use Jose\Component\Encryption\Algorithm\KeyEncryption\Chacha20Poly1305; |
25
|
|
|
use Jose\Component\Encryption\Algorithm\KeyEncryption\Dir; |
26
|
|
|
use Jose\Component\Encryption\Algorithm\KeyEncryption\ECDHES; |
27
|
|
|
use Jose\Component\Encryption\Algorithm\KeyEncryption\PBES2AESKW; |
28
|
|
|
use Jose\Component\Encryption\Algorithm\KeyEncryption\RSA; |
29
|
|
|
use Jose\Component\Encryption\JWEBuilderFactory; |
30
|
|
|
use Jose\Component\Encryption\JWEDecrypterFactory; |
31
|
|
|
use Jose\Component\Encryption\Serializer\JWESerializer as JWESerializerAlias; |
32
|
|
|
use Symfony\Component\Config\Definition\Builder\NodeDefinition; |
33
|
|
|
use Symfony\Component\Config\FileLocator; |
34
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
35
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
36
|
|
|
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader; |
37
|
|
|
|
38
|
|
|
class EncryptionSource implements SourceWithCompilerPasses |
39
|
|
|
{ |
40
|
|
|
/** |
41
|
|
|
* @var Source[] |
42
|
|
|
*/ |
43
|
|
|
private $sources; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* EncryptionSource constructor. |
47
|
|
|
*/ |
48
|
|
|
public function __construct() |
49
|
|
|
{ |
50
|
|
|
$this->sources = [ |
51
|
|
|
new JWEBuilder(), |
52
|
|
|
new JWEDecrypter(), |
53
|
|
|
new JWESerializer(), |
54
|
|
|
new JWELoader(), |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function name(): string |
59
|
|
|
{ |
60
|
|
|
return 'jwe'; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function load(array $configs, ContainerBuilder $container): void |
64
|
|
|
{ |
65
|
|
|
if (!$this->isEnabled()) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
$container->registerForAutoconfiguration(JWESerializerAlias::class)->addTag('jose.jwe.serializer'); |
69
|
|
|
$loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config')); |
70
|
|
|
$loader->load('jwe_services.php'); |
71
|
|
|
$loader->load('jwe_serializers.php'); |
72
|
|
|
$loader->load('compression_methods.php'); |
73
|
|
|
|
74
|
|
|
$loader = new PhpFileLoader($container, new FileLocator(__DIR__.'/../../../Resources/config/Algorithms/')); |
75
|
|
|
foreach ($this->getAlgorithmsFiles() as $class => $file) { |
76
|
|
|
if (class_exists($class)) { |
77
|
|
|
$loader->load($file); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if (\array_key_exists('jwe', $configs)) { |
82
|
|
|
foreach ($this->sources as $source) { |
83
|
|
|
$source->load($configs['jwe'], $container); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function getNodeDefinition(NodeDefinition $node): void |
89
|
|
|
{ |
90
|
|
|
if (!$this->isEnabled()) { |
91
|
|
|
return; |
92
|
|
|
} |
93
|
|
|
$childNode = $node->children() |
94
|
|
|
->arrayNode($this->name()) |
95
|
|
|
->addDefaultsIfNotSet() |
96
|
|
|
->treatFalseLike([]) |
97
|
|
|
->treatNullLike([]) |
98
|
|
|
; |
99
|
|
|
|
100
|
|
|
foreach ($this->sources as $source) { |
101
|
|
|
$source->getNodeDefinition($childNode); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function prepend(ContainerBuilder $container, array $config): array |
106
|
|
|
{ |
107
|
|
|
if (!$this->isEnabled()) { |
108
|
|
|
return []; |
109
|
|
|
} |
110
|
|
|
$result = []; |
111
|
|
|
foreach ($this->sources as $source) { |
112
|
|
|
$prepend = $source->prepend($container, $config); |
113
|
|
|
if (0 !== \count($prepend)) { |
114
|
|
|
$result[$source->name()] = $prepend; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $result; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @return CompilerPassInterface[] |
123
|
|
|
*/ |
124
|
|
|
public function getCompilerPasses(): array |
125
|
|
|
{ |
126
|
|
|
return [ |
127
|
|
|
new Compiler\EncryptionSerializerCompilerPass(), |
128
|
|
|
new Compiler\CompressionMethodCompilerPass(), |
129
|
|
|
]; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
private function getAlgorithmsFiles(): array |
133
|
|
|
{ |
134
|
|
|
$list = [ |
135
|
|
|
AESCBCHS::class => 'encryption_aescbc.php', |
136
|
|
|
AESGCM::class => 'encryption_aesgcm.php', |
137
|
|
|
AESGCMKW::class => 'encryption_aesgcmkw.php', |
138
|
|
|
AESKW::class => 'encryption_aeskw.php', |
139
|
|
|
Dir::class => 'encryption_dir.php', |
140
|
|
|
ECDHES::class => 'encryption_ecdhes.php', |
141
|
|
|
PBES2AESKW::class => 'encryption_pbes2.php', |
142
|
|
|
RSA::class => 'encryption_rsa.php', |
143
|
|
|
A128CTR::class => 'encryption_experimental.php', |
144
|
|
|
]; |
145
|
|
|
if (\in_array('chacha20-poly1305', openssl_get_cipher_methods(), true)) { |
146
|
|
|
$list[Chacha20Poly1305::class] = 'encryption_experimental_chacha20_poly1305.php'; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $list; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
private function isEnabled(): bool |
153
|
|
|
{ |
154
|
|
|
return class_exists(JWEBuilderFactory::class) && class_exists(JWEDecrypterFactory::class); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|