|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Platine Security |
|
5
|
|
|
* |
|
6
|
|
|
* Platine Security provides a complete security system with encryption, hash support |
|
7
|
|
|
* |
|
8
|
|
|
* This content is released under the MIT License (MIT) |
|
9
|
|
|
* |
|
10
|
|
|
* Copyright (c) 2020 Platine Security |
|
11
|
|
|
* Copyright (c) 2013 G4Code |
|
12
|
|
|
* |
|
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
15
|
|
|
* in the Software without restriction, including without limitation the rights |
|
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
18
|
|
|
* furnished to do so, subject to the following conditions: |
|
19
|
|
|
* |
|
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
21
|
|
|
* copies or substantial portions of the Software. |
|
22
|
|
|
* |
|
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
29
|
|
|
* SOFTWARE. |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @file OpenSSL.php |
|
34
|
|
|
* |
|
35
|
|
|
* The OpenSSL adapter class |
|
36
|
|
|
* |
|
37
|
|
|
* @package Platine\Security\Encryption |
|
38
|
|
|
* @author Platine Developers Team |
|
39
|
|
|
* @copyright Copyright (c) 2020 |
|
40
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
41
|
|
|
* @link https://www.platine-php.com |
|
42
|
|
|
* @version 1.0.0 |
|
43
|
|
|
* @filesource |
|
44
|
|
|
*/ |
|
45
|
|
|
|
|
46
|
|
|
declare(strict_types=1); |
|
47
|
|
|
|
|
48
|
|
|
namespace Platine\Security\Encryption; |
|
49
|
|
|
|
|
50
|
|
|
use Platine\Security\Exception\EncryptionException; |
|
51
|
|
|
use Exception; |
|
52
|
|
|
use RuntimeException; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @class OpenSSL |
|
56
|
|
|
* @package Platine\Security\Encryption |
|
57
|
|
|
*/ |
|
58
|
|
|
class OpenSSL implements AdapterInterface |
|
59
|
|
|
{ |
|
60
|
|
|
/** |
|
61
|
|
|
* OpenSSL cipher method constant |
|
62
|
|
|
* @var string |
|
63
|
|
|
*/ |
|
64
|
|
|
protected string $cipher = 'AES-256-CBC'; |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* OpenSSL input/output option constant |
|
68
|
|
|
* @var int |
|
69
|
|
|
*/ |
|
70
|
|
|
protected int $option = OPENSSL_RAW_DATA; |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Create new instance |
|
75
|
|
|
* @param array<string, mixed> $config |
|
76
|
|
|
*/ |
|
77
|
|
|
public function __construct(array $config = []) |
|
78
|
|
|
{ |
|
79
|
|
|
if (!extension_loaded('openssl')) { |
|
80
|
|
|
throw new RuntimeException( |
|
81
|
|
|
'OpenSSL extension is not loaded or actived, ' |
|
82
|
|
|
. 'please check your PHP configuration' |
|
83
|
|
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
if (isset($config['cipher']) && is_string($config['cipher'])) { |
|
87
|
|
|
$cipher = $config['cipher']; |
|
88
|
|
|
if (!in_array($cipher, openssl_get_cipher_methods())) { |
|
89
|
|
|
throw new EncryptionException(sprintf( |
|
90
|
|
|
'Invalid OpenSSL cipher [%s]', |
|
91
|
|
|
$cipher |
|
92
|
|
|
)); |
|
93
|
|
|
} else { |
|
94
|
|
|
$this->cipher = $cipher; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if (isset($config['option']) && is_int($config['option'])) { |
|
99
|
|
|
$option = $config['option']; |
|
100
|
|
|
$options = [ |
|
101
|
|
|
OPENSSL_RAW_DATA, |
|
102
|
|
|
OPENSSL_ZERO_PADDING |
|
103
|
|
|
]; |
|
104
|
|
|
|
|
105
|
|
|
if (in_array($option, $options) === false) { |
|
106
|
|
|
throw new EncryptionException(sprintf( |
|
107
|
|
|
'Invalid OpenSSL option [%d] must be one of [%s]', |
|
108
|
|
|
$option, |
|
109
|
|
|
implode(', ', $options) |
|
110
|
|
|
)); |
|
111
|
|
|
} else { |
|
112
|
|
|
$this->option = $option; |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* {@inhereitdoc} |
|
119
|
|
|
*/ |
|
120
|
|
|
public function createIV(int $size): string |
|
121
|
|
|
{ |
|
122
|
|
|
try { |
|
123
|
|
|
$bytes = openssl_random_pseudo_bytes($size); |
|
124
|
|
|
} catch (Exception $ex) { |
|
125
|
|
|
throw new EncryptionException( |
|
126
|
|
|
'Error occured when creating initialization vector' |
|
127
|
|
|
); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return $bytes; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* {@inhereitdoc} |
|
135
|
|
|
*/ |
|
136
|
|
|
public function decrypt(string $key, string $data, string $initVector): string |
|
137
|
|
|
{ |
|
138
|
|
|
$decrypted = openssl_decrypt( |
|
139
|
|
|
$data, |
|
140
|
|
|
$this->cipher, |
|
141
|
|
|
$key, |
|
142
|
|
|
$this->option, |
|
143
|
|
|
$initVector |
|
144
|
|
|
); |
|
145
|
|
|
|
|
146
|
|
|
if ($decrypted === false) { |
|
147
|
|
|
throw new EncryptionException( |
|
148
|
|
|
'Error occured when decrypting the data' |
|
149
|
|
|
); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $decrypted; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* {@inhereitdoc} |
|
157
|
|
|
*/ |
|
158
|
|
|
public function encrypt( |
|
159
|
|
|
string $key, |
|
160
|
|
|
string $data, |
|
161
|
|
|
string $initVector |
|
162
|
|
|
): string { |
|
163
|
|
|
$encrypted = openssl_encrypt( |
|
164
|
|
|
$data, |
|
165
|
|
|
$this->cipher, |
|
166
|
|
|
$key, |
|
167
|
|
|
$this->option, |
|
168
|
|
|
$initVector |
|
169
|
|
|
); |
|
170
|
|
|
|
|
171
|
|
|
if ($encrypted === false) { |
|
172
|
|
|
throw new EncryptionException( |
|
173
|
|
|
'Error occured when encrypting the data' |
|
174
|
|
|
); |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
return $encrypted; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* {@inhereitdoc} |
|
182
|
|
|
*/ |
|
183
|
|
|
public function getIVSize(): int |
|
184
|
|
|
{ |
|
185
|
|
|
$size = openssl_cipher_iv_length($this->cipher); |
|
186
|
|
|
|
|
187
|
|
|
if ($size === false) { |
|
188
|
|
|
throw new EncryptionException( |
|
189
|
|
|
'Error occured when get the initialization vector size' |
|
190
|
|
|
); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
return $size; |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|