@@ -255,7 +255,7 @@ private static function createLocalKeyObjectUsingPurePhpMethod(): array
255255 'x ' => Base64Url::encode (self ::addNullPadding ($ publicKey ->getPoint ()->getX ()->toBytes (false ))),
256256 'y ' => Base64Url::encode (self ::addNullPadding ($ publicKey ->getPoint ()->getY ()->toBytes (false ))),
257257 'd ' => Base64Url::encode (self ::addNullPadding ($ privateKey ->getSecret ()->toBytes (false ))),
258- ])
258+ ]),
259259 ];
260260 }
261261
@@ -266,7 +266,7 @@ private static function createLocalKeyObjectUsingPurePhpMethod(): array
266266 'x ' => Base64Url::encode (self ::addNullPadding (hex2bin (gmp_strval ($ publicKey ->getPoint ()->getX (), 16 )))),
267267 'y ' => Base64Url::encode (self ::addNullPadding (hex2bin (gmp_strval ($ publicKey ->getPoint ()->getY (), 16 )))),
268268 'd ' => Base64Url::encode (self ::addNullPadding (hex2bin (gmp_strval ($ privateKey ->getSecret (), 16 )))),
269- ])
269+ ]),
270270 ];
271271 }
272272
@@ -294,7 +294,7 @@ private static function createLocalKeyObjectUsingOpenSSL(): array
294294 'x ' => Base64Url::encode (self ::addNullPadding ($ details ['ec ' ]['x ' ])),
295295 'y ' => Base64Url::encode (self ::addNullPadding ($ details ['ec ' ]['y ' ])),
296296 'd ' => Base64Url::encode (self ::addNullPadding ($ details ['ec ' ]['d ' ])),
297- ])
297+ ]),
298298 ];
299299 }
300300
@@ -320,68 +320,14 @@ private static function getIKM(string $userAuthToken, string $userPublicKey, str
320320
321321 private static function calculateAgreementKey (JWK $ private_key , JWK $ public_key ): string
322322 {
323- if (function_exists ('openssl_pkey_derive ' )) {
324- try {
325- $ publicPem = ECKey::convertPublicKeyToPEM ($ public_key );
326- $ privatePem = ECKey::convertPrivateKeyToPEM ($ private_key );
327-
328- $ result = openssl_pkey_derive ($ publicPem , $ privatePem , 256 ); // @phpstan-ignore-line
329- if ($ result === false ) {
330- throw new \Exception ('Unable to compute the agreement key ' );
331- }
332- return $ result ;
333- } catch (\Throwable $ throwable ) {
334- //Does nothing. Will fallback to the pure PHP function
335- }
336- }
337-
323+ $ publicPem = ECKey::convertPublicKeyToPEM ($ public_key );
324+ $ privatePem = ECKey::convertPrivateKeyToPEM ($ private_key );
338325
339- $ curve = NistCurve::curve256 ();
340- try {
341- $ rec_x = self ::convertBase64ToBigInteger ($ public_key ->get ('x ' ));
342- $ rec_y = self ::convertBase64ToBigInteger ($ public_key ->get ('y ' ));
343- $ sen_d = self ::convertBase64ToBigInteger ($ private_key ->get ('d ' ));
344- $ priv_key = PrivateKey::create ($ sen_d );
345- $ pub_key = $ curve ->getPublicKeyFrom ($ rec_x , $ rec_y );
346-
347- return hex2bin (str_pad ($ curve ->mul ($ pub_key ->getPoint (), $ priv_key ->getSecret ())->getX ()->toBase (16 ), 64 , '0 ' , STR_PAD_LEFT )); // @phpstan-ignore-line
348- } catch (\Throwable $ e ) {
349- $ rec_x = self ::convertBase64ToGMP ($ public_key ->get ('x ' ));
350- $ rec_y = self ::convertBase64ToGMP ($ public_key ->get ('y ' ));
351- $ sen_d = self ::convertBase64ToGMP ($ private_key ->get ('d ' ));
352- $ priv_key = PrivateKey::create ($ sen_d ); // @phpstan-ignore-line
353- $ pub_key = $ curve ->getPublicKeyFrom ($ rec_x , $ rec_y ); // @phpstan-ignore-line
354-
355- return hex2bin (gmp_strval ($ curve ->mul ($ pub_key ->getPoint (), $ priv_key ->getSecret ())->getX (), 16 )); // @phpstan-ignore-line
326+ $ result = openssl_pkey_derive ($ publicPem , $ privatePem , 256 );
327+ if ($ result === false ) {
328+ throw new \Exception ('Unable to compute the agreement key ' );
356329 }
357- }
358-
359- /**
360- * @throws \ErrorException
361- */
362- private static function convertBase64ToBigInteger (string $ value ): BigInteger
363- {
364- $ value = unpack ('H* ' , Base64Url::decode ($ value ));
365-
366- if ($ value === false ) {
367- throw new \ErrorException ('Unable to unpack hex value from string ' );
368- }
369-
370- return BigInteger::fromBase ($ value [1 ], 16 );
371- }
372-
373- /**
374- * @throws \ErrorException
375- */
376- private static function convertBase64ToGMP (string $ value ): \GMP
377- {
378- $ value = unpack ('H* ' , Base64Url::decode ($ value ));
379-
380- if ($ value === false ) {
381- throw new \ErrorException ('Unable to unpack hex value from string ' );
382- }
383-
384- return gmp_init ($ value [1 ], 16 );
330+ return $ result ;
385331 }
386332
387333 private static function addNullPadding (string $ data ): string
0 commit comments