Repository navigation
Expand file tree
/
Copy pathSecureCrypt.php
More file actions
181 lines (153 loc) · 4.81 KB
/
Copy pathSecureCrypt.php
File metadata and controls
181 lines (153 loc) · 4.81 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
177
178
179
180
181
<?php
/**
* This code is licensed under AGPLv3 license or Afterlogic Software License
* if commercial version of the product was purchased.
* For full statements of the licenses see LICENSE-AFTERLOGIC and LICENSE-AGPL3 files.
*/
namespace Aurora\System;
class SecureCrypt
{
private const CIPHER = 'aes-256-gcm';
private const KEY_LEN = 32;
private const NONCE_LEN = 12;
private const TAG_LEN = 16;
/**
* New key through KDF — used only for V3 (AES-GCM)
*/
private static function getRawKey(): string
{
return hash_hkdf('sha256', Api::$sEncryptionKey, self::KEY_LEN, 'aurora-encrypt-v3');
}
/**
* Key in legacy format — only needed for decrypting legacy data
*/
private static function getLegacyKey()
{
return ctype_xdigit(Api::$sEncryptionKey)
? hex2bin(Api::$sEncryptionKey)
: Api::$sEncryptionKey;
}
/**
* Summary of EncryptValue
* @param string $sValue
* @return string|false
*/
public static function EncryptValue(string $sValue)
{
$key = self::getRawKey();
$nonce = random_bytes(self::NONCE_LEN);
$tag = '';
$cipher = openssl_encrypt(
$sValue,
self::CIPHER,
$key,
OPENSSL_RAW_DATA,
$nonce,
$tag,
'',
self::TAG_LEN
);
if ($cipher === false) {
return false;
}
return Utils::UrlSafeBase64Encode('V2' . $nonce . $tag . $cipher);
}
/**
* Tells whether the given encrypted value was produced by the legacy
* XXTEA encryption (used before AES-GCM). Legacy values encrypted via
* the old Casts\Encrypt had a 6-char random salt prepended to the
* plaintext; V2 (AES-GCM) values never did.
* @param string|null $sEncryptedValue
* @return bool
*/
public static function isLegacyFormat(?string $sEncryptedValue): bool
{
if ($sEncryptedValue === null || trim($sEncryptedValue) === '') {
return false;
}
$raw = Utils::UrlSafeBase64Decode(trim($sEncryptedValue));
if ($raw === false || $raw === '') {
return false;
}
return strncmp($raw, 'V2', 2) !== 0;
}
/**
* Summary of DecryptValue
* @param string $sEncryptedValue
* @return bool|string
*/
public static function DecryptValue(?string $sEncryptedValue)
{
if ($sEncryptedValue === null || trim($sEncryptedValue) === '') {
return false;
}
$raw = Utils::UrlSafeBase64Decode(trim($sEncryptedValue));
if ($raw === false || $raw === '') {
return false;
}
// New format: AES-256-GCM
if (strncmp($raw, 'V2', 2) === 0) {
return self::decryptV2($raw);
}
// Legacy: everything that was encrypted via XXTEA (V2 and the very old format)
return self::decryptLegacy($raw);
}
/**
* Summary of decryptV2
* @param string $raw
* @return bool|string
*/
private static function decryptV2(string $raw)
{
$minLen = 2 + self::NONCE_LEN + self::TAG_LEN;
if (strlen($raw) < $minLen) {
return false;
}
$nonce = substr($raw, 2, self::NONCE_LEN);
$tag = substr($raw, 2 + self::NONCE_LEN, self::TAG_LEN);
$cipherText = substr($raw, $minLen);
$key = self::getRawKey();
return openssl_decrypt(
$cipherText,
self::CIPHER,
$key,
OPENSSL_RAW_DATA,
$nonce,
$tag
);
}
/**
* Fully replicates the original DecryptValue logic,
* but is used only as a fallback for legacy data.
*/
private static function decryptLegacy(string $sEncryptedValue)
{
$mKey = self::getLegacyKey();
$sCryptKey = '$2y$07$' . Api::$sEncryptionKey . '$';
$aKeys = [
$mKey,
md5($sCryptKey),
$sCryptKey,
];
foreach ($aKeys as $key) {
$sValue = \Aurora\System\Utils\Crypt::XxteaDecrypt($sEncryptedValue, $key);
if ($sValue === false) {
continue;
}
// Completely old format — no version and no salt
self::logLegacyUsage('V1');
return $sValue;
}
return false;
}
/**
* Logs the usage of the legacy format to track migration.
* NEVER log the actual decrypted data (passwords, tokens).
* Goes through the app's own log (gated by the EnableLogging setting)
* instead of error_log, so it doesn't flood the web server's error log.
*/
private static function logLegacyUsage(string $format): void
{
Logger::Log(sprintf('[SecureCrypt] Legacy format "%s" decrypted', $format));
}
}