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.
111 lines
3.5 KiB
PHP
111 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Cloud;
|
|
|
|
/** Purpose-bound XChaCha20-Poly1305 sealing for each host Agent secret. */
|
|
final class CloudAgentSecretCipher
|
|
{
|
|
private const PREFIX = 'v1.xc20p.agent.';
|
|
private const AAD_PREFIX = 'virt-cloud:host-agent-secret:v1:';
|
|
|
|
public static function encrypt(string $secret, int $hostId): string
|
|
{
|
|
self::assertHostId($hostId);
|
|
if ($secret === '') {
|
|
throw new \InvalidArgumentException('Agent API secret cannot be empty');
|
|
}
|
|
self::assertAeadAvailable();
|
|
$nonce = random_bytes(
|
|
SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES
|
|
);
|
|
$cipher = sodium_crypto_aead_xchacha20poly1305_ietf_encrypt(
|
|
$secret,
|
|
self::AAD_PREFIX . $hostId,
|
|
$nonce,
|
|
self::key($hostId)
|
|
);
|
|
return self::PREFIX . rtrim(strtr(
|
|
base64_encode($nonce . $cipher),
|
|
'+/',
|
|
'-_'
|
|
), '=');
|
|
}
|
|
|
|
public static function decrypt(string $ciphertext, int $hostId): string
|
|
{
|
|
self::assertHostId($hostId);
|
|
if (!str_starts_with($ciphertext, self::PREFIX)) {
|
|
throw new CloudError(
|
|
'Agent API 密钥不是当前密封格式,请重新保存',
|
|
503
|
|
);
|
|
}
|
|
self::assertAeadAvailable();
|
|
$encoded = substr($ciphertext, strlen(self::PREFIX));
|
|
if ($encoded === ''
|
|
|| preg_match('/^[A-Za-z0-9_-]+$/D', $encoded) !== 1) {
|
|
throw new CloudError('Agent API 密钥密封数据格式无效', 503);
|
|
}
|
|
$padding = (4 - strlen($encoded) % 4) % 4;
|
|
$payload = base64_decode(
|
|
strtr($encoded, '-_', '+/') . str_repeat('=', $padding),
|
|
true
|
|
);
|
|
$nonceBytes = SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_NPUBBYTES;
|
|
if ($payload === false
|
|
|| strlen($payload) <= $nonceBytes
|
|
+ SODIUM_CRYPTO_AEAD_XCHACHA20POLY1305_IETF_ABYTES) {
|
|
throw new CloudError('Agent API 密钥密封数据格式无效', 503);
|
|
}
|
|
$plain = sodium_crypto_aead_xchacha20poly1305_ietf_decrypt(
|
|
substr($payload, $nonceBytes),
|
|
self::AAD_PREFIX . $hostId,
|
|
substr($payload, 0, $nonceBytes),
|
|
self::key($hostId)
|
|
);
|
|
if ($plain === false) {
|
|
throw new CloudError(
|
|
'Agent API 密钥认证失败,请检查 cloud_data_key',
|
|
503
|
|
);
|
|
}
|
|
return $plain;
|
|
}
|
|
|
|
private static function key(int $hostId): string
|
|
{
|
|
try {
|
|
$master = CloudSecrets::key('host-agent-secret');
|
|
} catch (\Throwable $exception) {
|
|
throw new CloudError(
|
|
'Agent API 密钥主密钥不可用,请配置 cloud_data_key 或 cloud_data_key_file',
|
|
503
|
|
);
|
|
}
|
|
return hash_hkdf(
|
|
'sha256',
|
|
$master,
|
|
32,
|
|
self::AAD_PREFIX . $hostId
|
|
);
|
|
}
|
|
|
|
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 assertHostId(int $hostId): void
|
|
{
|
|
if ($hostId < 1) {
|
|
throw new \InvalidArgumentException('Invalid host ID');
|
|
}
|
|
}
|
|
}
|