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

node:crypto generateKeyPair() is missing promisify argument metadata #26910

Closed
edtingen opened this issue Nov 18, 2024 · 0 comments · Fixed by #26913
Closed

node:crypto generateKeyPair() is missing promisify argument metadata #26910

edtingen opened this issue Nov 18, 2024 · 0 comments · Fixed by #26913

Comments

@edtingen
Copy link

Version: Deno 2.0.6

We had an error using deno with the npm package @google-cloud/cloud-sql-connector were we got an exception telling us that the public key was missing as an argument. That led to a longer debug session and us trying to find out how the library is creating public&private keys, which it is doing here https://github.com/GoogleCloudPlatform/cloud-sql-nodejs-connector/blob/main/src/crypto.ts#L73 using a promisified version of generateKeyPair().

According to the documentation it's fine and should work in both node and deno. But actually in deno privateKey and publicKey are undefined. This led us to more debugging on the internals of promisify. Normally the given callback style function would just get one argument for error and one for success, but generateKeyPair() is actually returning two arguments, which would get converted to an object, but in our case it's returning the first of those, which is the public key and not as an object.

The issue is that the customPromisifyArgs symbol is not defined on generateKeyPair() and therefore promisify fails to correctly convert the result to an object. What would be needed IMHO is defining the arguments as it's done with dns lookup() here

Object.defineProperty(lookup, customPromisifyArgs, {

Code to replicate:

more or less taken from https://docs.deno.com/api/node/crypto/~/generateKeyPair

import { promisify } from 'node:util';
const { generateKeyPair } = await import('node:crypto');

const gen = promisify(generateKeyPair);

const asyncResult = await gen('rsa', {
  modulusLength: 4096,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem',
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem',
    cipher: 'aes-256-cbc',
    passphrase: 'top secret',
  },
});
console.log(asyncResult);

generateKeyPair('rsa', {
  modulusLength: 4096,
  publicKeyEncoding: {
    type: 'spki',
    format: 'pem',
  },
  privateKeyEncoding: {
    type: 'pkcs8',
    format: 'pem',
    cipher: 'aes-256-cbc',
    passphrase: 'top secret',
  },
}, (...callbackArgs) => console.log(callbackArgs));

With deno 2.0.6 asyncResult is just a string with the public key. callbackArgs is an array like [null, publicKey, privateKey]

With nodejs asyncResult is an object {publicKey, privateKey}, while callbackArgs is the same as with deno.


Sidenote: giving credit to @sb-ma who spotted it first.

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 a pull request may close this issue.

1 participant