Skip to content

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

Description

@gnarea

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

Activity

  1. panva commented on Nov 3, 2022

    @panva
    Member

    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'}); // 🎉 
  2. added
    cryptoIssues and PRs related to the crypto subsystem.
    on Nov 3, 2022
  3. gnarea commented on Nov 3, 2022

    @gnarea
    Author

    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. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    cryptoIssues and PRs related to the crypto subsystem.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions