["curve_name" => "secp384r1"]]); if($serverPrivateKey === false){ throw new \RuntimeException("openssl_pkey_new() failed: " . openssl_error_string()); } self::$SERVER_PRIVATE_KEY = $serverPrivateKey; } $this->serverPrivateKey = igbinary_serialize(openssl_pkey_get_details(self::$SERVER_PRIVATE_KEY)); $this->clientPub = $clientPub; $this->storeLocal(self::TLS_KEY_ON_COMPLETION, $onCompletion); } public function onRun() : void{ /** @var mixed[] $serverPrivDetails */ $serverPrivDetails = igbinary_unserialize($this->serverPrivateKey); $serverPriv = openssl_pkey_new($serverPrivDetails); if($serverPriv === false) throw new AssumptionFailedError("Failed to restore server signing key from details"); $clientPub = JwtUtils::parseDerPublicKey($this->clientPub); $sharedSecret = EncryptionUtils::generateSharedSecret($serverPriv, $clientPub); $salt = random_bytes(16); $this->aesKey = EncryptionUtils::generateKey($sharedSecret, $salt); $this->handshakeJwt = EncryptionUtils::generateServerHandshakeJwt($serverPriv, $salt); openssl_free_key($serverPriv); openssl_free_key($clientPub); } public function onCompletion() : void{ /** * @var \Closure $callback * @phpstan-var \Closure(string $encryptionKey, string $handshakeJwt) : void $callback */ $callback = $this->fetchLocal(self::TLS_KEY_ON_COMPLETION); if($this->aesKey === null || $this->handshakeJwt === null){ throw new AssumptionFailedError("Something strange happened here ..."); } $callback($this->aesKey, $this->handshakeJwt); } }