-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEncryption.php
More file actions
176 lines (149 loc) · 5.61 KB
/
Copy pathEncryption.php
File metadata and controls
176 lines (149 loc) · 5.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
declare(strict_types=1);
namespace Rancoud\Session;
trait Encryption
{
protected ?string $key = null;
protected string $method = 'aes-256-cbc';
public function setKey(string $key): void
{
$this->key = $key;
}
/** @throws SessionException */
public function setMethod(string $method): void
{
if (!\in_array($method, $this->getAvailableMethods(), true)) {
throw new SessionException(\sprintf('Unknown method: %s', $method));
}
$this->method = $method;
}
public function getAvailableMethods(): array
{
$ciphers = \openssl_get_cipher_methods();
$ciphersAndAliases = \openssl_get_cipher_methods(true);
$cipherAliases = \array_diff($ciphersAndAliases, $ciphers);
$ciphers = \array_filter($ciphers, static function ($n) {
$excludeMethods = [
'ecb', 'des', 'rc2', 'rc4', 'md5',
'-ocb', '-ccm', '-gcm', '-wrap'
];
foreach ($excludeMethods as $excludeMethod) {
if (\mb_stripos($n, $excludeMethod) !== false) {
return false;
}
}
return true;
});
$cipherAliases = \array_filter($cipherAliases, static function ($c) {
$excludeMethods = [
'des', 'rc2', '-wrap', 'id-'
];
foreach ($excludeMethods as $excludeMethod) {
if (\mb_stripos($c, $excludeMethod) !== false) {
return false;
}
}
return true;
});
$methods = \array_merge($ciphers, $cipherAliases);
return \array_filter($methods, static function ($c) {
$forbiddenMethods = [
'aes-128-cbc-hmac-sha1',
'aes-256-cbc-hmac-sha1',
'aes-128-cbc-cts',
'aes-128-cbc-hmac-sha256',
'aes-128-siv',
'aes-192-cbc-cts',
'aes-192-siv',
'aes-256-cbc-cts',
'aes-256-cbc-hmac-sha256',
'aes-256-siv',
'camellia-128-cbc-cts',
'camellia-192-cbc-cts',
'camellia-256-cbc-cts',
'null',
'sm4-xts',
'0.3.4401.5.3.1.9.1',
'0.3.4401.5.3.1.9.21',
'0.3.4401.5.3.1.9.41',
'1.2.410.200046.1.1.1',
'1.2.410.200046.1.1.11',
'1.2.410.200046.1.1.34',
'1.2.410.200046.1.1.35',
'1.2.410.200046.1.1.36',
'1.2.410.200046.1.1.37',
'1.2.410.200046.1.1.38',
'1.2.410.200046.1.1.39',
'1.2.410.200046.1.1.6',
'1.2.156.10197.1.104.10',
'1.2.840.113549.1.9.16.3.6',
'1.3.14.3.2.17',
'2.16.840.1.101.3.4.1.1',
'2.16.840.1.101.3.4.1.21',
'2.16.840.1.101.3.4.1.25',
'2.16.840.1.101.3.4.1.26',
'2.16.840.1.101.3.4.1.27',
'2.16.840.1.101.3.4.1.41',
'2.16.840.1.101.3.4.1.45',
'2.16.840.1.101.3.4.1.46',
'2.16.840.1.101.3.4.1.47',
'2.16.840.1.101.3.4.1.5',
'2.16.840.1.101.3.4.1.6',
'2.16.840.1.101.3.4.1.7',
'1.2.156.10197.1.104.1',
'1.2.156.10197.1.104.8',
'1.2.156.10197.1.104.9',
'chacha20-poly1305'
];
return !\in_array(\mb_strtolower($c), $forbiddenMethods, true);
});
}
/** @throws SessionException */
public function decrypt(string $data): string
{
$this->throwExceptionIfKeyEmpty();
if ($data === '') {
return '';
}
[$encryptedData, $iv] = \explode('::', \base64_decode($data, true), 2);
$dataDecrypted = \openssl_decrypt($encryptedData, $this->method, $this->key, 0, $iv);
if ($dataDecrypted === false) {
// @codeCoverageIgnoreStart
// Could not reach this statement without mocking the function
throw new SessionException('Could not decrypt with openssl_decrypt');
// @codeCoverageIgnoreEnd
}
return $dataDecrypted;
}
/** @throws SessionException */
public function encrypt(string $data): string
{
$this->throwExceptionIfKeyEmpty();
try {
$length = \openssl_cipher_iv_length($this->method);
if ($length === false || $length < 1) {
throw new SessionException('IV generation failed');
}
} catch (\Exception) {
throw new SessionException('IV generation failed');
}
/** @noinspection CryptographicallySecureRandomnessInspection */
$iv = \openssl_random_pseudo_bytes($length, $cstrong);
/** @noinspection PhpStrictComparisonWithOperandsOfDifferentTypesInspection */
if ($iv === false || $cstrong === false) {
// @codeCoverageIgnoreStart
// Could not reach this statement without mocking the function
throw new SessionException('IV generation failed');
// @codeCoverageIgnoreEnd
}
$encrypted = \openssl_encrypt($data, $this->method, $this->key, 0, $iv);
return \base64_encode($encrypted . '::' . $iv);
}
/** @throws SessionException */
protected function throwExceptionIfKeyEmpty(): void
{
if ($this->key === null || $this->key === '') {
throw new SessionException('Key has to be a non-empty string');
}
}
}