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.
50 lines
2.8 KiB
PHP
50 lines
2.8 KiB
PHP
<?php
|
|
namespace App\Services {
|
|
final class Config {
|
|
public static string $key = '';
|
|
public static function get($name, $default = null) { return $name === 'cloud_data_key' ? self::$key : $default; }
|
|
}
|
|
}
|
|
namespace {
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
if (!function_exists('sodium_crypto_aead_xchacha20poly1305_ietf_encrypt')) {
|
|
echo "SKIPPED CloudSecretsTest: runtime lacks sodium; production requires sodium.\n";
|
|
exit(0);
|
|
}
|
|
use App\Services\Config;
|
|
use App\Services\Cloud\CloudSecrets;
|
|
use App\Services\Cloud\CloudError;
|
|
use App\Services\TicketTelegramCredentialCipher;
|
|
Config::$key = base64_encode(str_repeat('k', 32));
|
|
$value = ['password' => 'test-fixture-password'];
|
|
$first = CloudSecrets::encrypt($value, 'test-record');
|
|
$second = CloudSecrets::encrypt($value, 'test-record');
|
|
if ($first === $second || !str_starts_with($first, 'v1.xc20p.')
|
|
|| str_contains($first, $value['password'])
|
|
|| CloudSecrets::decrypt($first, 'test-record') !== $value) {
|
|
throw new RuntimeException('Encryption, random nonce or roundtrip failed.');
|
|
}
|
|
$prefix = 'v1.xc20p.';
|
|
$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 = $prefix . rtrim(strtr(base64_encode($raw), '+/', '-_'), '=');
|
|
try { CloudSecrets::decrypt($tampered, 'test-record'); throw new RuntimeException('Tampered ciphertext accepted.'); } catch (CloudError $expected) {}
|
|
try { CloudSecrets::decrypt($first, 'another-purpose'); throw new RuntimeException('Cross-purpose ciphertext accepted.'); } catch (CloudError $expected) {}
|
|
try { CloudSecrets::decrypt(base64_encode('retired'), 'test-record'); throw new RuntimeException('Retired ciphertext format accepted.'); } catch (CloudError $expected) {}
|
|
|
|
$telegramToken = '123456789:' . str_repeat('A', 35);
|
|
$telegramTokenCipher = TicketTelegramCredentialCipher::encrypt($telegramToken, 'bot_token');
|
|
$webhookSecret = str_repeat('b', 48);
|
|
$webhookSecretCipher = TicketTelegramCredentialCipher::encrypt($webhookSecret, 'webhook_secret');
|
|
if (TicketTelegramCredentialCipher::decrypt($telegramTokenCipher, 'bot_token') !== $telegramToken
|
|
|| TicketTelegramCredentialCipher::decrypt($webhookSecretCipher, 'webhook_secret') !== $webhookSecret) {
|
|
throw new RuntimeException('Telegram credential purpose derivation or roundtrip failed.');
|
|
}
|
|
|
|
Config::$key = base64_encode(str_repeat('x', 32));
|
|
try { CloudSecrets::decrypt($first, 'test-record'); throw new RuntimeException('Wrong key accepted.'); } catch (CloudError $expected) {}
|
|
echo "Cloud XChaCha20 nonce, purpose binding, authentication and key isolation passed.\n";
|
|
}
|