Skip to content

Commit 3b32ed8

Browse files
committed
change content-encoding and added some comments
1 parent 83ee6c9 commit 3b32ed8

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/Encryption.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,23 +52,27 @@ public static function encrypt($payload, $userPublicKey, $userAuthToken, $native
5252
// generate salt
5353
$salt = openssl_random_pseudo_bytes(16);
5454

55-
$prk = !empty($userAuthToken) ?
56-
self::hkdf($userAuthToken, $sharedSecret, utf8_decode('Content-Encoding: auth\0'), 32) :
55+
// section 4.3
56+
$ikm = !empty($userAuthToken) ?
57+
self::hkdf($userAuthToken, $sharedSecret, 'Content-Encoding: auth'.chr(0), 32) :
5758
$sharedSecret;
5859

60+
// section 4.2
5961
$context = self::createContext($userPublicKey, $localPublicKey);
6062

6163
// derive the Content Encryption Key
62-
$contentEncryptionKeyInfo = self::createInfo('aesgcm', $context);
63-
$contentEncryptionKey = self::hkdf($salt, $prk, $contentEncryptionKeyInfo, 16);
64+
// TODO Chrome GCM wants 'aesgcm'?
65+
$contentEncryptionKeyInfo = self::createInfo('aesgcm128', $context);
66+
$contentEncryptionKey = self::hkdf($salt, $ikm, $contentEncryptionKeyInfo, 16);
6467

65-
// derive the Nonce
68+
// section 3.3, derive the nonce
6669
$nonceInfo = self::createInfo('nonce', $context);
67-
$nonce = self::hkdf($salt, $prk, $nonceInfo, 12);
70+
$nonce = self::hkdf($salt, $ikm, $nonceInfo, 12);
6871

6972
// encrypt
73+
// "The additional data passed to each invocation of AEAD_AES_128_GCM is a zero-length octet sequence."
7074
if (!$nativeEncryption) {
71-
list($encryptedText, $tag) = \Jose\Util\GCM::encrypt($contentEncryptionKey, $nonce, $plaintext, "");
75+
list($encryptedText, $tag) = \Jose\Util\GCM::encrypt($contentEncryptionKey, $nonce, $plaintext, null);
7276
$cipherText = $encryptedText.$tag;
7377
} else {
7478
$cipherText = openssl_encrypt($plaintext, 'aes-128-gcm', $contentEncryptionKey, false, $nonce); // base 64 encoded
@@ -150,6 +154,7 @@ private static function createInfo($type, $context) {
150154
throw new \ErrorException('Context argument has invalid size');
151155
}
152156

157+
// TODO Why 'P-256'?
153158
return 'Content-Encoding: '.$type.chr(0).'P-256'.$context;
154159
}
155160
}

src/WebPush.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ private function sendToStandardEndpoints(array $notifications)
196196
$headers = array(
197197
'Content-Length' => strlen($encrypted['cipherText']),
198198
'Content-Type' => 'application/octet-stream',
199-
'Content-Encoding' => 'aesgcm',
199+
'Content-Encoding' => 'aesgcm128',
200200
'Encryption' => 'keyid="p256dh";salt="'.$encrypted['salt'].'"',
201201
'Crypto-Key' => 'keyid="p256dh";dh="'.$encrypted['localPublicKey'].'"',
202202
'TTL' => $this->TTL,

0 commit comments

Comments
 (0)