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.
60 lines
2.1 KiB
PHP
60 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services {
|
|
final class Config
|
|
{
|
|
public static string $dataKey = '';
|
|
|
|
public static function get($name, $default = null)
|
|
{
|
|
return $name === 'cloud_data_key' ? self::$dataKey : $default;
|
|
}
|
|
}
|
|
}
|
|
|
|
namespace {
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use App\Services\Config;
|
|
use App\Services\Cloud\CloudAgentSecretCipher;
|
|
use App\Services\Cloud\CloudError;
|
|
|
|
if (!function_exists('sodium_crypto_aead_xchacha20poly1305_ietf_encrypt')) {
|
|
echo "SKIPPED CloudAgentSecretCipherTest: runtime lacks sodium.\n";
|
|
exit(0);
|
|
}
|
|
|
|
Config::$dataKey = base64_encode(str_repeat('p', 32));
|
|
$secret = 'agent-secret-with-more-than-thirty-two-bytes';
|
|
$first = CloudAgentSecretCipher::encrypt($secret, 11);
|
|
$second = CloudAgentSecretCipher::encrypt($secret, 11);
|
|
if ($first === $second || str_contains($first, $secret)
|
|
|| CloudAgentSecretCipher::decrypt($first, 11) !== $secret) {
|
|
throw new RuntimeException('Agent secret encryption or nonce isolation failed.');
|
|
}
|
|
try {
|
|
CloudAgentSecretCipher::decrypt($first, 12);
|
|
throw new RuntimeException('Agent secret decrypted under another host identity.');
|
|
} catch (CloudError $expected) {
|
|
}
|
|
$prefix = 'v1.xc20p.agent.';
|
|
$encoded = substr($first, strlen($prefix));
|
|
$raw = base64_decode(strtr($encoded, '-_', '+/')
|
|
. str_repeat('=', (4 - strlen($encoded) % 4) % 4), true);
|
|
$raw[strlen($raw) - 1] = chr(ord($raw[strlen($raw) - 1]) ^ 1);
|
|
$tampered = rtrim(strtr(base64_encode($raw), '+/', '-_'), '=');
|
|
try {
|
|
CloudAgentSecretCipher::decrypt($prefix . $tampered, 11);
|
|
throw new RuntimeException('Tampered Agent secret ciphertext was accepted.');
|
|
} catch (CloudError $expected) {
|
|
}
|
|
|
|
try {
|
|
CloudAgentSecretCipher::decrypt('sbox:agent:v1:' . base64_encode('old'), 11);
|
|
throw new RuntimeException('Retired Agent ciphertext format was accepted.');
|
|
} catch (CloudError $expected) {
|
|
}
|
|
|
|
echo "Agent secret data-key derivation, host isolation and authentication passed.\n";
|
|
}
|