Skip to content

Commit b4c78aa

Browse files
authored
fix: RSA from JWK sometimes returns empty Instance (#628)
1 parent e41f1bd commit b4c78aa

1 file changed

Lines changed: 8 additions & 0 deletions

File tree

src/JWK.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,14 @@ private static function createPemFromModulusAndExponent(
240240
): string {
241241
$mod = JWT::urlsafeB64Decode($n);
242242
$exp = JWT::urlsafeB64Decode($e);
243+
// Correct encoding for ASN1, as ints are represented as unsigned in jwk
244+
// but signed in ASN1. Prepending null byte makes it unsigned.
245+
if (\strlen($mod) > 0 && \ord($mod[0]) >= 128) {
246+
$mod = \chr(0) . $mod;
247+
}
248+
if (\strlen($exp) > 0 && \ord($exp[0]) >= 128) {
249+
$exp = \chr(0) . $exp;
250+
}
243251

244252
$modulus = \pack('Ca*a*', 2, self::encodeLength(\strlen($mod)), $mod);
245253
$publicExponent = \pack('Ca*a*', 2, self::encodeLength(\strlen($exp)), $exp);

0 commit comments

Comments
 (0)