1313
1414namespace Minishlink \WebPush ;
1515
16- use Base64Url \Base64Url ;
1716use Jose \Component \Core \AlgorithmManager ;
1817use Jose \Component \Core \JWK ;
1918use Jose \Component \KeyManagement \JWKFactory ;
2019use Jose \Component \Signature \Algorithm \ES256 ;
2120use Jose \Component \Signature \JWSBuilder ;
2221use Jose \Component \Signature \Serializer \CompactSerializer ;
22+ use ParagonIE \ConstantTime \Base64UrlSafe ;
2323
2424class VAPID
2525{
@@ -54,14 +54,14 @@ public static function validate(array $vapid): array
5454 throw new \ErrorException ('Failed to convert VAPID public key from hexadecimal to binary ' );
5555 }
5656 $ vapid ['publicKey ' ] = base64_encode ($ binaryPublicKey );
57- $ vapid ['privateKey ' ] = base64_encode (str_pad (Base64Url:: decode ($ jwk ->get ('d ' )), 2 * self ::PRIVATE_KEY_LENGTH , '0 ' , STR_PAD_LEFT ));
57+ $ vapid ['privateKey ' ] = base64_encode (str_pad (Base64UrlSafe:: decodeNoPadding ($ jwk ->get ('d ' )), 2 * self ::PRIVATE_KEY_LENGTH , '0 ' , STR_PAD_LEFT ));
5858 }
5959
6060 if (!isset ($ vapid ['publicKey ' ])) {
6161 throw new \ErrorException ('[VAPID] You must provide a public key. ' );
6262 }
6363
64- $ publicKey = Base64Url:: decode ($ vapid ['publicKey ' ]);
64+ $ publicKey = Base64UrlSafe:: decodeNoPadding ($ vapid ['publicKey ' ]);
6565
6666 if (Utils::safeStrlen ($ publicKey ) !== self ::PUBLIC_KEY_LENGTH ) {
6767 throw new \ErrorException ('[VAPID] Public key should be 65 bytes long when decoded. ' );
@@ -71,7 +71,7 @@ public static function validate(array $vapid): array
7171 throw new \ErrorException ('[VAPID] You must provide a private key. ' );
7272 }
7373
74- $ privateKey = Base64Url:: decode ($ vapid ['privateKey ' ]);
74+ $ privateKey = Base64UrlSafe:: decodeNoPadding ($ vapid ['privateKey ' ]);
7575
7676 if (Utils::safeStrlen ($ privateKey ) !== self ::PRIVATE_KEY_LENGTH ) {
7777 throw new \ErrorException ('[VAPID] Private key should be 32 bytes long when decoded. ' );
@@ -122,9 +122,9 @@ public static function getVapidHeaders(string $audience, string $subject, string
122122 $ jwk = new JWK ([
123123 'kty ' => 'EC ' ,
124124 'crv ' => 'P-256 ' ,
125- 'x ' => Base64Url:: encode ($ x ),
126- 'y ' => Base64Url:: encode ($ y ),
127- 'd ' => Base64Url:: encode ($ privateKey ),
125+ 'x ' => Base64UrlSafe:: encodeUnpadded ($ x ),
126+ 'y ' => Base64UrlSafe:: encodeUnpadded ($ y ),
127+ 'd ' => Base64UrlSafe:: encodeUnpadded ($ privateKey ),
128128 ]);
129129
130130 $ jwsCompactSerializer = new CompactSerializer ();
@@ -136,7 +136,7 @@ public static function getVapidHeaders(string $audience, string $subject, string
136136 ->build ();
137137
138138 $ jwt = $ jwsCompactSerializer ->serialize ($ jws , 0 );
139- $ encodedPublicKey = Base64Url:: encode ($ publicKey );
139+ $ encodedPublicKey = Base64UrlSafe:: encodeUnpadded ($ publicKey );
140140
141141 if ($ contentEncoding === "aesgcm " ) {
142142 return [
@@ -169,14 +169,14 @@ public static function createVapidKeys(): array
169169 throw new \ErrorException ('Failed to convert VAPID public key from hexadecimal to binary ' );
170170 }
171171
172- $ binaryPrivateKey = hex2bin (str_pad (bin2hex (Base64Url:: decode ($ jwk ->get ('d ' ))), 2 * self ::PRIVATE_KEY_LENGTH , '0 ' , STR_PAD_LEFT ));
172+ $ binaryPrivateKey = hex2bin (str_pad (bin2hex (Base64UrlSafe:: decodeNoPadding ($ jwk ->get ('d ' ))), 2 * self ::PRIVATE_KEY_LENGTH , '0 ' , STR_PAD_LEFT ));
173173 if (!$ binaryPrivateKey ) {
174174 throw new \ErrorException ('Failed to convert VAPID private key from hexadecimal to binary ' );
175175 }
176176
177177 return [
178- 'publicKey ' => Base64Url:: encode ($ binaryPublicKey ),
179- 'privateKey ' => Base64Url:: encode ($ binaryPrivateKey ),
178+ 'publicKey ' => Base64UrlSafe:: encodeUnpadded ($ binaryPublicKey ),
179+ 'privateKey ' => Base64UrlSafe:: encodeUnpadded ($ binaryPrivateKey ),
180180 ];
181181 }
182182}
0 commit comments