56 lines
2.1 KiB
PHP
56 lines
2.1 KiB
PHP
|
|
<?php
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Regressionstest für die Web-Push-Verschlüsselung (RFC 8291, Anhang A).
|
||
|
|
*
|
||
|
|
* php tools/test_webpush_rfc8291.php
|
||
|
|
*
|
||
|
|
* Prüft insbesondere die leere AAD nach RFC 8188. Wird der Header als AAD
|
||
|
|
* verwendet, akzeptiert der Push-Dienst die Nachricht, der Browser verwirft
|
||
|
|
* sie aber still.
|
||
|
|
*/
|
||
|
|
|
||
|
|
require __DIR__ . '/../app/bootstrap.php';
|
||
|
|
|
||
|
|
function test_b64u_decode(string $value): string
|
||
|
|
{
|
||
|
|
$value = preg_replace('/\s+/', '', $value) ?? '';
|
||
|
|
$value = strtr($value, '-_', '+/');
|
||
|
|
$value .= str_repeat('=', (4 - strlen($value) % 4) % 4);
|
||
|
|
return base64_decode($value, true);
|
||
|
|
}
|
||
|
|
|
||
|
|
function test_b64u_encode(string $value): string
|
||
|
|
{
|
||
|
|
return rtrim(strtr(base64_encode($value), '+/', '-_'), '=');
|
||
|
|
}
|
||
|
|
|
||
|
|
$plaintext = base64_decode('V2hlbiBJIGdyb3cgdXAsIEkgd2FudCB0byBiZSBhIHdhdGVybWVsb24=');
|
||
|
|
$asPublic = test_b64u_decode('BP4z9KsN6nGRTbVYI_c7VJSPQTBtkgcy27mlmlMoZIIgDll6e3vCYLocInmYWAmS6TlzAC8wEqKK6PBru3jl7A8');
|
||
|
|
$asPrivate = test_b64u_decode('yfWPiYE-n46HLnH0KqZOF1fJJU3MYrct3AELtAQ-oRw');
|
||
|
|
$uaPublic = test_b64u_decode('BCVxsr7N_eNgVRqvHtD0zTZsEc6-VV-JvLexhqUzORcxaOzi6-AYWXvTBHm4bjyPjs7Vd8pZGH6SRpkNtoIAiw4');
|
||
|
|
$salt = test_b64u_decode('DGv6ra1nlYgDCS1FRnbzlw');
|
||
|
|
$auth = test_b64u_decode('BTBZMqHH6r4Tts7J_aSIgg');
|
||
|
|
|
||
|
|
$expected = 'DGv6ra1nlYgDCS1FRnbzlwAAEABBBP4z9KsN6nGRTbVYI_c7VJSPQTBtkgcy27ml'
|
||
|
|
. 'mlMoZIIgDll6e3vCYLocInmYWAmS6TlzAC8wEqKK6PBru3jl7A_yl95bQpu6cVPT'
|
||
|
|
. 'pK4Mqgkf1CXztLVBSt2Ks3oZwbuwXPXLWyouBWLVWGNWQexSgSxsj_Qulcy4a-fN';
|
||
|
|
|
||
|
|
$method = new ReflectionMethod(WebPush::class, 'privateKeyPem');
|
||
|
|
$method->setAccessible(true);
|
||
|
|
$ephemeral = openssl_pkey_get_private($method->invoke(null, $asPrivate, $asPublic));
|
||
|
|
if (!$ephemeral) {
|
||
|
|
fwrite(STDERR, "Der Server-Schlüssel aus dem RFC ließ sich nicht laden.\n");
|
||
|
|
exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
$body = WebPush::encrypt($plaintext, test_b64u_encode($uaPublic), test_b64u_encode($auth), $salt, $ephemeral);
|
||
|
|
if (test_b64u_encode($body) === $expected) {
|
||
|
|
echo "OK: RFC-8291-Testvektor stimmt.\n";
|
||
|
|
exit(0);
|
||
|
|
}
|
||
|
|
|
||
|
|
fwrite(STDERR, "FEHLER: Der verschlüsselte Body weicht vom RFC-8291-Testvektor ab.\n");
|
||
|
|
exit(1);
|