Skip to content

fix: RSA from JWK sometimes returns empty Instance#628

Merged
bshaffer merged 2 commits intofirebase:mainfrom
NReib:main
Mar 31, 2026
Merged

fix: RSA from JWK sometimes returns empty Instance#628
bshaffer merged 2 commits intofirebase:mainfrom
NReib:main

Conversation

@NReib
Copy link
Copy Markdown

@NReib NReib commented Mar 27, 2026

#589 addresses specification difference between JWK (or JWA) and ASN1 integers.

See https://datatracker.ietf.org/doc/html/rfc7518#section-6.3.1.1 for the n parameter these kinds of null bytes are stripped and need to be added again.

firebase#589 addresses specification difference between JWK (or JWA) and ASN1 integers.
@google-cla
Copy link
Copy Markdown

google-cla bot commented Mar 27, 2026

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request updates the createPemFromModulusAndExponent method in src/JWK.php to ensure correct ASN1 encoding by prepending a null byte to the modulus and exponent if their first byte is 128 or greater. Feedback was provided regarding styling inconsistencies, such as the use of tabs instead of spaces and missing spaces after control keywords, to maintain consistency with the rest of the codebase.

Copy link
Copy Markdown
Collaborator

@bshaffer bshaffer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for finding this tricky issue! This looks good, but I can't seem to write a test to key cover this. I have tried using the key supplied in #589, but no error is thrown. Do you know how we can test that this behavior fixes the issue?

Thanks again!

@NReib
Copy link
Copy Markdown
Author

NReib commented Mar 31, 2026

@bshaffer sure, using the JWK from the issue, currently we will end up with the following pem. #589 describes the resulting key object to be "empty", I did not really check the details, but after applying the fix, I was able to verify the signature as I intended to do.

-----BEGIN PUBLIC KEY-----
MIIBITANBgkqhkiG9w0BAQEFAAOCAQ4AMIIBCQKCAQDIqoZFDIm2rFGb8yjZhMII
CQ9INvu42ALu1eOBXM2/b9cHyvi8yJsGlkU9vDJVSTTAaE+TyouOsiP8rwgy78hi
2SrzulzX/UUA65vt7fFSszHroYKrxAb3jFE5J8Na4zLu3AgvCNuTM0RTps2JszQh
BsK93JsU+Grhge+XA6N+4yXaY1SZrzBPX+XYxl6dXV2Z4tOD+RgZ708aaf0mJji3
TU2PUUlCP2sfzrcoBrctp1DiXxoE6lohnCMA2jcYIkl3i5IHrsb6nTC64MNhuJQa
03nl16BeMMqu/e/lsuCk8xfrKe+3ZuLSiVv9Bl+i9OdRot+xmkJdOT8K9RevZDYr
AgMBAAE=
-----END PUBLIC KEY-----

While correctly encoded, it should be this

-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyKqGRQyJtqxRm/Mo2YTC
CAkPSDb7uNgC7tXjgVzNv2/XB8r4vMibBpZFPbwyVUk0wGhPk8qLjrIj/K8IMu/I
Ytkq87pc1/1FAOub7e3xUrMx66GCq8QG94xROSfDWuMy7twILwjbkzNEU6bNibM0
IQbCvdybFPhq4YHvlwOjfuMl2mNUma8wT1/l2MZenV1dmeLTg/kYGe9PGmn9JiY4
t01Nj1FJQj9rH863KAa3LadQ4l8aBOpaIZwjANo3GCJJd4uSB67G+p0wuuDDYbiU
GtN55degXjDKrv3v5bLgpPMX6ynvt2bi0olb/QZfovTnUaLfsZpCXTk/CvUXr2Q2
KwIDAQAB
-----END PUBLIC KEY-----

@bshaffer bshaffer merged commit b4c78aa into firebase:main Mar 31, 2026
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants