You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
127 lines
4.4 KiB
PHP
127 lines
4.4 KiB
PHP
<?php
|
|
namespace App\Services\Cloud;
|
|
|
|
use App\Services\Config;
|
|
|
|
/** Versioned PHP-owned data sealing with mandatory purpose-bound AAD. */
|
|
final class CloudSecrets
|
|
{
|
|
private const PREFIX = 'v1.xc20p.';
|
|
private const CONTEXT = 'virt-cloud:data-seal:v1:';
|
|
|
|
/** Return a purpose-separated 256-bit key derived from cloud_data_key. */
|
|
public static function key(string $purpose): string
|
|
{
|
|
self::assertPurpose($purpose);
|
|
return hash_hkdf(
|
|
'sha256',
|
|
self::masterKey(),
|
|
32,
|
|
'virt-cloud:key:v1:' . $purpose
|
|
);
|
|
}
|
|
|
|
public static function encrypt(array $payload, string $purpose): string
|
|
{
|
|
self::assertAeadAvailable();
|
|
$nonce = random_bytes(
|
|
SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES
|
|
);
|
|
$plain = json_encode(
|
|
$payload,
|
|
JSON_THROW_ON_ERROR | JSON_UNESCAPED_UNICODE
|
|
| JSON_UNESCAPED_SLASHES
|
|
);
|
|
$cipher = sodium_crypto_aead_xchacha20poly1305_ietf_encrypt(
|
|
$plain,
|
|
self::CONTEXT . $purpose,
|
|
$nonce,
|
|
self::key('seal-' . $purpose)
|
|
);
|
|
return self::PREFIX . self::base64UrlEncode($nonce . $cipher);
|
|
}
|
|
|
|
public static function decrypt(string $ciphertext, string $purpose): array
|
|
{
|
|
self::assertAeadAvailable();
|
|
if (!str_starts_with($ciphertext, self::PREFIX)) {
|
|
throw new CloudError('加密数据不是当前版本,请重建全新数据库', 503);
|
|
}
|
|
$raw = self::base64UrlDecode(substr($ciphertext, strlen(self::PREFIX)));
|
|
$nonceBytes = SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES;
|
|
$tagBytes = SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES;
|
|
if ($raw === null || strlen($raw) <= $nonceBytes + $tagBytes) {
|
|
throw new CloudError('加密数据格式无效', 503);
|
|
}
|
|
$plain = sodium_crypto_aead_xchacha20poly1305_ietf_decrypt(
|
|
substr($raw, $nonceBytes),
|
|
self::CONTEXT . $purpose,
|
|
substr($raw, 0, $nonceBytes),
|
|
self::key('seal-' . $purpose)
|
|
);
|
|
if ($plain === false) {
|
|
throw new CloudError('加密数据认证失败', 503);
|
|
}
|
|
try {
|
|
$value = json_decode($plain, true, 32, JSON_THROW_ON_ERROR);
|
|
} catch (\Throwable $error) {
|
|
throw new CloudError('加密数据内容无效', 503);
|
|
}
|
|
if (!is_array($value) || array_is_list($value)) {
|
|
throw new CloudError('加密数据内容无效', 503);
|
|
}
|
|
return $value;
|
|
}
|
|
|
|
private static function masterKey(): string
|
|
{
|
|
$value = trim((string) Config::get('cloud_data_key', ''));
|
|
if ($value === '') {
|
|
$path = (string) Config::get('cloud_data_key_file', '');
|
|
if ($path === '' || str_contains($path, "\0")
|
|
|| !is_file($path) || !is_readable($path)) {
|
|
throw new CloudError('PHP 数据密封主密钥尚未配置', 503);
|
|
}
|
|
$value = trim((string) file_get_contents($path));
|
|
}
|
|
$key = base64_decode($value, true);
|
|
if ($key === false || strlen($key) !== 32) {
|
|
throw new CloudError('PHP 数据密封主密钥格式无效', 503);
|
|
}
|
|
return $key;
|
|
}
|
|
|
|
private static function assertPurpose(string $purpose): void
|
|
{
|
|
if (preg_match('/^[a-z][a-z0-9.-]{2,63}$/D', $purpose) !== 1) {
|
|
throw new \InvalidArgumentException('Invalid data-seal purpose');
|
|
}
|
|
}
|
|
|
|
private static function assertAeadAvailable(): void
|
|
{
|
|
if (!function_exists('sodium_crypto_aead_xchacha20poly1305_ietf_encrypt')
|
|
|| !function_exists('sodium_crypto_aead_xchacha20poly1305_ietf_decrypt')) {
|
|
throw new CloudError('需要 PHP sodium XChaCha20-Poly1305 支持', 503);
|
|
}
|
|
}
|
|
|
|
private static function base64UrlEncode(string $value): string
|
|
{
|
|
return rtrim(strtr(base64_encode($value), '+/', '-_'), '=');
|
|
}
|
|
|
|
private static function base64UrlDecode(string $value): ?string
|
|
{
|
|
if ($value === '' || preg_match('/^[A-Za-z0-9_-]+$/D', $value) !== 1) {
|
|
return null;
|
|
}
|
|
$padding = (4 - strlen($value) % 4) % 4;
|
|
$decoded = base64_decode(
|
|
strtr($value, '-_', '+/') . str_repeat('=', $padding),
|
|
true
|
|
);
|
|
return $decoded === false ? null : $decoded;
|
|
}
|
|
}
|