gmp_strval(gmp_import($str, 1, GMP_BIG_ENDIAN | GMP_MSW_FIRST), 10); $sequence = new Sequence( new Integer($convert($rString)), new Integer($convert($sString)) ); $v = openssl_verify( $header . '.' . $body, $sequence->getBinary(), $signingKey, OPENSSL_ALGO_SHA384 ); switch($v){ case 0: return false; case 1: return true; case -1: throw new JwtException("Error verifying JWT signature: " . openssl_error_string()); default: throw new AssumptionFailedError("openssl_verify() should only return -1, 0 or 1"); } } /** * @phpstan-param array $header * @phpstan-param array $claims */ public static function create(array $header, array $claims, \OpenSSLAsymmetricKey $signingKey) : string{ $jwtBody = JwtUtils::b64UrlEncode(json_encode($header, JSON_THROW_ON_ERROR)) . "." . JwtUtils::b64UrlEncode(json_encode($claims, JSON_THROW_ON_ERROR)); openssl_sign( $jwtBody, $rawDerSig, $signingKey, OPENSSL_ALGO_SHA384 ); try{ $asnObject = Sequence::fromBinary($rawDerSig); }catch(ParserException $e){ throw new AssumptionFailedError("Failed to parse OpenSSL signature: " . $e->getMessage(), 0, $e); } if(count($asnObject) !== 2){ throw new AssumptionFailedError("OpenSSL produced invalid signature, expected exactly 2 parts"); } [$r, $s] = [$asnObject[0], $asnObject[1]]; if(!($r instanceof Integer) || !($s instanceof Integer)){ throw new AssumptionFailedError("OpenSSL produced invalid signature, expected 2 INTEGER parts"); } $rString = $r->getContent(); $sString = $s->getContent(); $toBinary = fn($str) => str_pad( gmp_export(gmp_init($str, 10), 1, GMP_BIG_ENDIAN | GMP_MSW_FIRST), 48, "\x00", STR_PAD_LEFT ); $jwtSig = JwtUtils::b64UrlEncode($toBinary($rString) . $toBinary($sString)); return "$jwtBody.$jwtSig"; } public static function b64UrlEncode(string $str) : string{ return rtrim(strtr(base64_encode($str), '+/', '-_'), '='); } public static function b64UrlDecode(string $str) : string{ if(($len = strlen($str) % 4) !== 0){ $str .= str_repeat('=', 4 - $len); } $decoded = base64_decode(strtr($str, '-_', '+/'), true); if($decoded === false){ throw new JwtException("Malformed base64url encoded payload could not be decoded"); } return $decoded; } public static function emitDerPublicKey(\OpenSSLAsymmetricKey $opensslKey) : string{ $details = Utils::assumeNotFalse(openssl_pkey_get_details($opensslKey), "Failed to get details from OpenSSL key resource"); /** @var string $pemKey */ $pemKey = $details['key']; if(preg_match("@^-----BEGIN[A-Z\d ]+PUBLIC KEY-----\n([A-Za-z\d+/\n]+)\n-----END[A-Z\d ]+PUBLIC KEY-----\n$@", $pemKey, $matches) === 1){ $derKey = base64_decode(str_replace("\n", "", $matches[1]), true); if($derKey !== false){ return $derKey; } } throw new AssumptionFailedError("OpenSSL resource contains invalid public key"); } public static function parseDerPublicKey(string $derKey) : \OpenSSLAsymmetricKey{ $signingKeyOpenSSL = openssl_pkey_get_public(sprintf("-----BEGIN PUBLIC KEY-----\n%s\n-----END PUBLIC KEY-----\n", base64_encode($derKey))); if($signingKeyOpenSSL === false){ throw new JwtException("OpenSSL failed to parse key: " . openssl_error_string()); } return $signingKeyOpenSSL; } }