Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RSA key in JWK format can't be imported with createPublicKey() #45304

Closed
gnarea opened this issue Nov 3, 2022 · 2 comments
Closed

RSA key in JWK format can't be imported with createPublicKey() #45304

gnarea opened this issue Nov 3, 2022 · 2 comments
Labels
crypto Issues and PRs related to the crypto subsystem.

Comments

@gnarea
Copy link

gnarea commented Nov 3, 2022

Version

v16.13.0

Platform

Linux relaybox 5.15.0-52-generic #58-Ubuntu SMP Thu Oct 13 08:03:55 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

Subsystem

crypto

What steps will reproduce the bug?

// Taken from https://www.rfc-editor.org/rfc/rfc5702.html#section-6.1
const modulus = 'wVwaxrHF2CK64aYKRUibLiH30KpPuPBjel7E8ZydQW1HYWHfoGmidzC2RnhwCC293hCzw+TFR2nqn8OVSY5t2Q==';
const exponent = 'AQAB';
createPublicKey({key: { n: modulus, e: exponent }, format: 'jwk'}); // Error thrown; see below

How often does it reproduce? Is there a required condition?

Always

What is the expected behavior?

PublicKeyObject instance is returned.

What do you see instead?

TypeError: The "key.key" property must be of type string or an instance of ArrayBuffer, Buffer, TypedArray, DataView, KeyObject, or CryptoKey. Received an instance of Object
    at new NodeError (node:internal/errors:371:5)
    at prepareAsymmetricKey (node:internal/crypto/keys:528:13)
    at createPublicKey (node:internal/crypto/keys:589:5)
    at Object.<anonymous> (/home/gus/repos/dnssec-verifier-js/src/lib/utils/crypto.spec.ts:53:25)
    at Promise.then.completed (/home/gus/repos/dnssec-verifier-js/node_modules/jest-circus/build/utils.js:333:28)
    at new Promise (<anonymous>)
    at callAsyncCircusFn (/home/gus/repos/dnssec-verifier-js/node_modules/jest-circus/build/utils.js:259:10)
    at _callCircusTest (/home/gus/repos/dnssec-verifier-js/node_modules/jest-circus/build/run.js:277:40)
    at _runTest (/home/gus/repos/dnssec-verifier-js/node_modules/jest-circus/build/run.js:209:3)
    at _runTestsForDescribeBlock (/home/gus/repos/dnssec-verifier-js/node_modules/jest-circus/build/run.js:97:9)

However, key must be an object since we're trying to import the key by specifying its exponent and modulus.

Additional information

No response

@panva
Copy link
Member

panva commented Nov 3, 2022

Hey Gus ;)

The key.key must be a valid JWK for the import to kick in (FYI the latest v16.x would also give you a more useful error message).

const modulus = 'wVwaxrHF2CK64aYKRUibLiH30KpPuPBjel7E8ZydQW1HYWHfoGmidzC2RnhwCC293hCzw+TFR2nqn8OVSY5t2Q==';
const exponent = 'AQAB';
const kty = 'RSA'
createPublicKey({key: { n: modulus, e: exponent, kty }, format: 'jwk'}); // 🎉 

@panva panva added the crypto Issues and PRs related to the crypto subsystem. label Nov 3, 2022
@gnarea
Copy link
Author

gnarea commented Nov 3, 2022

Hey Filip! 👋🏾 I hope you're well! 🙌🏾

Thank you so much for looking into this!. Indeed, adding the kty made it all work as expected! I'll close the (non-)issue in that case. :)

@gnarea gnarea closed this as not planned Won't fix, can't repro, duplicate, stale Nov 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crypto Issues and PRs related to the crypto subsystem.
Projects
None yet
Development

No branches or pull requests

2 participants