@@ -60,7 +60,12 @@ public static function validate(array $vapid): array
60
60
gmp_init (bin2hex (Base64Url::decode ($ jwk ->get ('x ' ))), 16 ),
61
61
gmp_init (bin2hex (Base64Url::decode ($ jwk ->get ('y ' ))), 16 )
62
62
));
63
- $ vapid ['publicKey ' ] = base64_encode (hex2bin (Utils::serializePublicKey ($ publicKey )));
63
+
64
+ $ binaryPublicKey = hex2bin (Utils::serializePublicKey ($ publicKey ));
65
+ if (!$ binaryPublicKey ) {
66
+ throw new \ErrorException ('Failed to convert VAPID public key from hexadecimal to binary ' );
67
+ }
68
+ $ vapid ['publicKey ' ] = base64_encode ($ binaryPublicKey );
64
69
$ vapid ['privateKey ' ] = base64_encode (str_pad (Base64Url::decode ($ jwk ->get ('d ' )), 2 * self ::PRIVATE_KEY_LENGTH , '0 ' , STR_PAD_LEFT ));
65
70
}
66
71
@@ -122,6 +127,9 @@ public static function getVapidHeaders(string $audience, string $subject, string
122
127
'exp ' => $ expiration ,
123
128
'sub ' => $ subject ,
124
129
], JSON_UNESCAPED_SLASHES | JSON_NUMERIC_CHECK );
130
+ if (!$ jwtPayload ) {
131
+ throw new \ErrorException ('Failed to encode JWT payload in JSON ' );
132
+ }
125
133
126
134
list ($ x , $ y ) = Utils::unserializePublicKey ($ publicKey );
127
135
$ jwk = JWK ::create ([
@@ -163,16 +171,27 @@ public static function getVapidHeaders(string $audience, string $subject, string
163
171
* DO NOT create keys at each initialization! Save those keys and reuse them.
164
172
*
165
173
* @return array
174
+ * @throws \ErrorException
166
175
*/
167
176
public static function createVapidKeys (): array
168
177
{
169
178
$ curve = NistCurve::curve256 ();
170
179
$ privateKey = $ curve ->createPrivateKey ();
171
180
$ publicKey = $ curve ->createPublicKey ($ privateKey );
172
181
182
+ $ binaryPublicKey = hex2bin (Utils::serializePublicKey ($ publicKey ));
183
+ if (!$ binaryPublicKey ) {
184
+ throw new \ErrorException ('Failed to convert VAPID public key from hexadecimal to binary ' );
185
+ }
186
+
187
+ $ binaryPrivateKey = hex2bin (str_pad (gmp_strval ($ privateKey ->getSecret (), 16 ), 2 * self ::PRIVATE_KEY_LENGTH , '0 ' , STR_PAD_LEFT ));
188
+ if (!$ binaryPrivateKey ) {
189
+ throw new \ErrorException ('Failed to convert VAPID private key from hexadecimal to binary ' );
190
+ }
191
+
173
192
return [
174
- 'publicKey ' => base64_encode (hex2bin (Utils:: serializePublicKey ( $ publicKey )) ),
175
- 'privateKey ' => base64_encode (hex2bin ( str_pad ( gmp_strval ( $ privateKey -> getSecret (), 16 ), 2 * self :: PRIVATE_KEY_LENGTH , ' 0 ' , STR_PAD_LEFT )) )
193
+ 'publicKey ' => base64_encode ($ binaryPublicKey ),
194
+ 'privateKey ' => base64_encode ($ binaryPrivateKey )
176
195
];
177
196
}
178
197
}
0 commit comments