Skip to content

Commit c09b8e2

Browse files
authored
Fix encryption in v3 (#146)
* Don't fail silently PushServiceTest * Fix encryption
1 parent 5c98dbf commit c09b8e2

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

Diff for: src/Encryption.php

+13-4
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,21 @@ public static function encrypt(string $payload, string $userPublicKey, string $u
5050
$userAuthToken = Base64Url::decode($userAuthToken);
5151

5252
$curve = NistCurve::curve256();
53-
$privateKey = $curve->createPrivateKey();
54-
$publicKey = $curve->createPublicKey($privateKey);
55-
$localPublicKey = hex2bin(Utils::serializePublicKey($publicKey));
53+
54+
// get local key pair
55+
$localPrivateKeyObject = $curve->createPrivateKey();
56+
$localPublicKeyObject = $curve->createPublicKey($localPrivateKeyObject);
57+
$localPublicKey = hex2bin(Utils::serializePublicKey($localPublicKeyObject));
58+
59+
// get user public key object
60+
[$userPublicKeyObjectX, $userPublicKeyObjectY] = Utils::unserializePublicKey($userPublicKey);
61+
$userPublicKeyObject = $curve->getPublicKeyFrom(
62+
gmp_init(bin2hex($userPublicKeyObjectX), 16),
63+
gmp_init(bin2hex($userPublicKeyObjectY), 16)
64+
);
5665

5766
// get shared secret from user public key and local private key
58-
$sharedSecret = $curve->mul($publicKey->getPoint(), $privateKey->getSecret())->getX();
67+
$sharedSecret = $curve->mul($userPublicKeyObject->getPoint(), $localPrivateKeyObject->getSecret())->getX();
5968
$sharedSecret = hex2bin(gmp_strval($sharedSecret, 16));
6069

6170
// generate salt

Diff for: tests/PushServiceTest.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,6 @@ protected function createClosureTest($browserId, $browserVersion, $options)
161161
$p256dh = $keys->{'p256dh'};
162162

163163
$payload = 'hello';
164-
$getNotificationCurl = null;
165164
try {
166165
$sendResp = $this->webPush->sendNotification($endpoint, $payload, $p256dh, $auth, true);
167166
$this->assertTrue($sendResp);
@@ -183,7 +182,7 @@ protected function createClosureTest($browserId, $browserVersion, $options)
183182
CURLOPT_TIMEOUT => self::$timeout,
184183
]);
185184

186-
$parsedResp = $this->getResponse($getSubscriptionCurl);
185+
$parsedResp = $this->getResponse($getNotificationCurl);
187186

188187
if (!property_exists($parsedResp->{'data'}, 'messages')) {
189188
throw new Exception('web-push-testing-service error, no messages: '.json_encode($parsedResp));
@@ -199,6 +198,8 @@ protected function createClosureTest($browserId, $browserVersion, $options)
199198
echo $e;
200199
}
201200
$this->assertEquals($e->getMessage(), 'No GCM API Key specified.');
201+
} else {
202+
throw $e;
202203
}
203204
}
204205
};

0 commit comments

Comments
 (0)