From 1890dfcc4763a49bbc517a47421ece2471b54b87 Mon Sep 17 00:00:00 2001 From: Cyperghost Date: Mon, 19 Feb 2024 14:09:50 +0100 Subject: [PATCH] Remove converting from bin2hex and hex2bin. Using directly the raw values --- .../system/service/worker/Encryption.class.php | 2 +- .../worker/ServiceWorkerHandler.class.php | 2 +- .../lib/system/service/worker/Util.class.php | 16 ++++++++-------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/wcfsetup/install/files/lib/system/service/worker/Encryption.class.php b/wcfsetup/install/files/lib/system/service/worker/Encryption.class.php index bc5114fb93..9d3a421b72 100644 --- a/wcfsetup/install/files/lib/system/service/worker/Encryption.class.php +++ b/wcfsetup/install/files/lib/system/service/worker/Encryption.class.php @@ -43,7 +43,7 @@ final class Encryption $userJwk = Util::createJWK($x, $y); // application-server $newJwk = JWKFactory::createECKey(Encryption::CURVE_ALGORITHM); - $newPublicKey = \hex2bin(Util::serializePublicKey($newJwk->get('x'), $newJwk->get('y'))); + $newPublicKey = Util::serializePublicKey($newJwk->get('x'), $newJwk->get('y')); \assert($newPublicKey, "Failed to serialize public key"); $sharedSecret = Encryption::getSharedSecret($userJwk, $newJwk); diff --git a/wcfsetup/install/files/lib/system/service/worker/ServiceWorkerHandler.class.php b/wcfsetup/install/files/lib/system/service/worker/ServiceWorkerHandler.class.php index ba5251749e..f517993d4d 100644 --- a/wcfsetup/install/files/lib/system/service/worker/ServiceWorkerHandler.class.php +++ b/wcfsetup/install/files/lib/system/service/worker/ServiceWorkerHandler.class.php @@ -39,7 +39,7 @@ final class ServiceWorkerHandler extends SingletonFactory public static function createNewKeys(): void { $jwk = JWKFactory::createECKey(Encryption::CURVE_ALGORITHM); - $binaryPublicKey = \hex2bin(Util::serializePublicKey($jwk->get('x'), $jwk->get('y'))); + $binaryPublicKey = Util::serializePublicKey($jwk->get('x'), $jwk->get('y')); $binaryPrivateKey = \hex2bin( \str_pad(\bin2hex(Base64Url::decode($jwk->get('d'))), 2 * VAPID::PRIVATE_KEY_LENGTH, '0', STR_PAD_LEFT) ); diff --git a/wcfsetup/install/files/lib/system/service/worker/Util.class.php b/wcfsetup/install/files/lib/system/service/worker/Util.class.php index 0a2a495c29..ecfd87d89f 100644 --- a/wcfsetup/install/files/lib/system/service/worker/Util.class.php +++ b/wcfsetup/install/files/lib/system/service/worker/Util.class.php @@ -22,24 +22,24 @@ final class Util */ public static function unserializePublicKey(string $data): array { - $data = \bin2hex($data); - if (Binary::safeSubstr($data, 0, 2) !== '04') { - throw new \InvalidArgumentException('Invalid public key format'); + if (\mb_strlen($data, '8bit') !== VAPID::PUBLIC_KEY_LENGTH || $data[0] !== "\x04") { + throw new \InvalidArgumentException('Invalid public key format.'); } - $data = Binary::safeSubstr($data, 2); + $data = \mb_substr($data, 1, null, '8bit'); $dataLength = \mb_strlen($data, '8bit'); + [$x, $y] = \mb_str_split($data, \intdiv($dataLength, 2), '8bit'); return [ - 'x' => \hex2bin(Binary::safeSubstr($data, 0, $dataLength / 2)), - 'y' => \hex2bin(Binary::safeSubstr($data, $dataLength / 2)), + 'x' => $x, + 'y' => $y, ]; } /** * Serialize the public key. * - * @param string $x - * @param string $y + * @param string $x encoded base64 x coordinate + * @param string $y encoded base64 y coordinate * @return string */ public static function serializePublicKey(string $x, string $y): string -- 2.20.1