Web-Push: AAD nach RFC 8188 auf leer korrigieren
This commit is contained in:
parent
1089f4bb03
commit
46d05acae5
2 changed files with 75 additions and 4 deletions
|
|
@ -274,9 +274,11 @@ class WebPush {
|
|||
* @param string $payload
|
||||
* @param string $p256dh base64url des Geräteschlüssels (65 Byte)
|
||||
* @param string $auth base64url des Auth-Geheimnisses (16 Byte)
|
||||
* @param string|null $salt Nur für Tests: feste Salt statt Zufall
|
||||
* @param \OpenSSLAsymmetricKey|resource|null $ephemeral Nur für Tests: festes Server-Schlüsselpaar
|
||||
* @return string Binärer Request-Body
|
||||
*/
|
||||
public static function encrypt($payload, $p256dh, $auth) {
|
||||
public static function encrypt($payload, $p256dh, $auth, $salt = null, $ephemeral = null) {
|
||||
$uaPublic = self::base64UrlDecode($p256dh);
|
||||
$authSecret = self::base64UrlDecode($auth);
|
||||
if ($uaPublic === false || strlen($uaPublic) !== 65) {
|
||||
|
|
@ -287,7 +289,9 @@ class WebPush {
|
|||
}
|
||||
|
||||
// Einmaliges Server-Schlüsselpaar für diese Nachricht
|
||||
$ephemeral = openssl_pkey_new(['curve_name' => 'prime256v1', 'private_key_type' => OPENSSL_KEYTYPE_EC]);
|
||||
if ($ephemeral === null) {
|
||||
$ephemeral = openssl_pkey_new(['curve_name' => 'prime256v1', 'private_key_type' => OPENSSL_KEYTYPE_EC]);
|
||||
}
|
||||
if (!$ephemeral) {
|
||||
throw new \RuntimeException('Temporärer EC-Schlüssel konnte nicht erzeugt werden.');
|
||||
}
|
||||
|
|
@ -306,14 +310,19 @@ class WebPush {
|
|||
$prkKey = hash_hmac('sha256', $shared, $authSecret, true);
|
||||
$ikm = self::hkdfExpand($prkKey, "WebPush: info\x00" . $uaPublic . $asPublic, 32);
|
||||
|
||||
$salt = random_bytes(16);
|
||||
$salt = $salt === null ? random_bytes(16) : $salt;
|
||||
if (strlen($salt) !== 16) {
|
||||
throw new \InvalidArgumentException('Salt muss 16 Byte lang sein.');
|
||||
}
|
||||
$prk = hash_hmac('sha256', $ikm, $salt, true);
|
||||
$cek = self::hkdfExpand($prk, "Content-Encoding: aes128gcm\x00", 16);
|
||||
$nonce = self::hkdfExpand($prk, "Content-Encoding: nonce\x00", 12);
|
||||
|
||||
$plain = $payload . "\x02";
|
||||
$header = $salt . pack('N', self::RECORD_SIZE) . chr(65) . $asPublic;
|
||||
$cipher = openssl_encrypt($plain, 'aes-128-gcm', $cek, OPENSSL_RAW_DATA, $nonce, $tag, $header, 16);
|
||||
// RFC 8188, Abschnitt 2.1: AAD ist ausdrücklich leer. Der Header wird
|
||||
// nur übertragen, nicht authentifiziert.
|
||||
$cipher = openssl_encrypt($plain, 'aes-128-gcm', $cek, OPENSSL_RAW_DATA, $nonce, $tag, '', 16);
|
||||
if ($cipher === false) {
|
||||
throw new \RuntimeException('Verschlüsselung fehlgeschlagen.');
|
||||
}
|
||||
|
|
|
|||
62
tools/test_webpush_rfc8291.php
Normal file
62
tools/test_webpush_rfc8291.php
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
<?php
|
||||
/**
|
||||
* Regressionstest: App\Core\WebPush::encrypt gegen den Testvektor aus
|
||||
* RFC 8291, Anhang A.
|
||||
*
|
||||
* Aufruf mit PHP CLI:
|
||||
* php tools/test_webpush_rfc8291.php
|
||||
*
|
||||
* Der Test prüft insbesondere die AAD: RFC 8188 schreibt für `aes128gcm`
|
||||
* eine leere Additional Authenticated Data vor. Wird stattdessen der Header
|
||||
* als AAD verwendet, entsteht ein anderer GCM-Tag. Der Push-Dienst nimmt die
|
||||
* Nachricht dann trotzdem mit HTTP 201 an, der Browser verwirft sie aber
|
||||
* stillschweigend – genau dieser Fehler war am 04.11.2026 die Ursache dafür,
|
||||
* dass Test-Erinnerungen nicht ankamen.
|
||||
*/
|
||||
|
||||
require __DIR__ . '/../tippspiel/src/autoload.php';
|
||||
|
||||
use App\Core\WebPush;
|
||||
|
||||
function test_b64u_decode($value) {
|
||||
$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($value) {
|
||||
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);
|
||||
$actual = test_b64u_encode($body);
|
||||
|
||||
if ($actual === $expected) {
|
||||
echo "OK: RFC-8291-Testvektor stimmt.\n";
|
||||
exit(0);
|
||||
}
|
||||
|
||||
fwrite(STDERR, "FEHLER: Der verschlüsselte Body weicht vom RFC-8291-Testvektor ab.\n");
|
||||
fwrite(STDERR, "erwartet: $expected\n");
|
||||
fwrite(STDERR, "erhalten: $actual\n");
|
||||
exit(1);
|
||||
Loading…
Add table
Add a link
Reference in a new issue