@@ -40,6 +40,12 @@ class WebPush
40
40
/** @var int Time To Live of notifications */
41
41
private $ TTL ;
42
42
43
+ /** @var boolean */
44
+ private $ payloadEncryptionSupport ;
45
+
46
+ /** @var boolean */
47
+ private $ nativePayloadEncryptionSupport ;
48
+
43
49
/**
44
50
* WebPush constructor.
45
51
*
@@ -56,6 +62,9 @@ public function __construct(array $apiKeys = array(), $TTL = 2419200, $timeout =
56
62
$ client = isset ($ client ) ? $ client : new MultiCurl ();
57
63
$ client ->setTimeout ($ timeout );
58
64
$ this ->browser = new Browser ($ client );
65
+
66
+ $ this ->payloadEncryptionSupport = version_compare (phpversion (), '5.5.9 ' , '>= ' );
67
+ $ this ->nativePayloadEncryptionSupport = version_compare (phpversion (), '7.1 ' , '>= ' );
59
68
}
60
69
61
70
/**
@@ -186,11 +195,11 @@ private function encrypt($userPublicKey, $payload)
186
195
$ salt = openssl_random_pseudo_bytes (16 );
187
196
188
197
// get encryption key
189
- $ encryptionKey = hash_hmac ('sha256 ' , $ salt , $ sharedSecret );
198
+ $ encryptionKey = hash_hmac ('sha256 ' , $ salt , $ sharedSecret, true );
190
199
191
200
// encrypt
192
201
$ iv = openssl_random_pseudo_bytes (openssl_cipher_iv_length ('aes-128-gcm ' ));
193
- if (phpversion () < 7.1 ) {
202
+ if (! $ this -> nativePayloadEncryptionSupport ) {
194
203
list ($ encryptedText , $ tag ) = GCM ::encrypt ($ encryptionKey , $ iv , $ payload , "" );
195
204
$ cipherText = $ encryptedText .$ tag ;
196
205
} else {
@@ -200,7 +209,7 @@ private function encrypt($userPublicKey, $payload)
200
209
return array (
201
210
'localPublicKey ' => $ localPublicKey ,
202
211
'salt ' => base64_encode ($ salt ),
203
- 'cipherText ' => $ cipherText ,
212
+ 'cipherText ' => base64_encode ( $ cipherText) ,
204
213
);
205
214
}
206
215
@@ -212,15 +221,15 @@ private function sendToStandardEndpoints(array $notifications)
212
221
$ payload = $ notification ->getPayload ();
213
222
$ userPublicKey = $ notification ->getUserPublicKey ();
214
223
215
- if (isset ($ payload ) && isset ($ userPublicKey )) {
224
+ if (isset ($ payload ) && isset ($ userPublicKey ) && $ this -> payloadEncryptionSupport ) {
216
225
$ encrypted = $ this ->encrypt ($ userPublicKey , $ payload );
217
226
218
227
$ headers = array (
219
228
'Content-Length ' => strlen ($ encrypted ['cipherText ' ]),
220
229
'Content-Type ' => 'application/octet-stream ' ,
221
- 'Encryption-Key ' => 'keyid=p256dh;dh= ' . $ encrypted [ ' localPublicKey ' ] ,
222
- 'Encryption ' => 'keyid=p256dh;salt= ' .$ encrypted ['salt ' ],
223
- 'Content-Encoding ' => 'aesgcm128 ' ,
230
+ 'Content-Encoding ' => 'aesgcm-128 ' ,
231
+ 'Encryption ' => 'keyid=" p256dh" ;salt=" ' .$ encrypted ['salt ' ]. ' " ' ,
232
+ 'Encryption-Key ' => 'keyid="p256dh";dh=" ' . $ encrypted [ ' localPublicKey ' ]. ' " ' ,
224
233
'TTL ' => $ this ->TTL ,
225
234
);
226
235
0 commit comments