Skip to content

Using JWK as encoding format for public key in crypto.generateKeyPairSync() throws an error #39205

Closed
@johannesnydahl

Description

@johannesnydahl
  • Version: v16.4.0
  • Platform: Microsoft Windows NT 10.0.18363.0 x64
  • Subsystem: Crypto

What steps will reproduce the bug?

main.js

const crypto = require("crypto");

const keys = crypto.generateKeyPairSync("ec", {
	namedCurve: "P-384",
	publicKeyEncoding: { type: "spki", format: "jwk" },
	privateKeyEncoding: { type: "pkcs8", format: "pem" }
});

console.log(keys.publicKey);

then just execute the command node main.js

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

Everytime. If I change the public key encoding format to "pem" or "der" it dosen't throw an error. It seems like "jwk" is the only broken format.

What is the expected behavior?

Should probably look something like the output below. I used the keyObject.export() to create this output because as mentioned above, crypto.generateKeyPairSync() dosen't work at all with "jwk".

Code to produce the expected output:

const crypto = require("crypto");

const keys = crypto.generateKeyPairSync("ec", {
	namedCurve: "P-384"
});

console.log(keys.publicKey.export({ format: "jwk" }));

Expected output:

{
  crv: 'P-384',
  kty: 'EC',
  x: 'BC-y_ZyH6rYMv_YAREfUainM1c9iwhHc9KEPPRD1u2zGJMuGV1LvEWh2igD3kAS5',
  y: 'LX7TofWICe4_nJZNBkS0rtqDCDoTp9_TuKHqKQHh1wDH3hvgSZjPWZN1TnrvbfR4'
}

What do you see instead?

node:internal/crypto/keys:268
  throw new ERR_INVALID_ARG_VALUE(optionName, formatStr);
  ^

TypeError [ERR_INVALID_ARG_VALUE]: The property 'options.publicKeyEncoding.format' is invalid. Received 'jwk'
    at new NodeError (node:internal/errors:363:5)
    at parseKeyFormat (node:internal/crypto/keys:268:9)
    at parseKeyFormatAndType (node:internal/crypto/keys:304:18)
    at parseKeyEncoding (node:internal/crypto/keys:332:7)
    at parsePublicKeyEncoding (node:internal/crypto/keys:371:10)
    at parseKeyEncoding (node:internal/crypto/keygen:125:9)
    at createJob (node:internal/crypto/keygen:161:42)
    at Object.generateKeyPairSync (node:internal/crypto/keygen:95:22)
    at Object.<anonymous> (C:\Users\johannes.nydahl\Desktop\Playground\main.js:3:21)
    at Module._compile (node:internal/modules/cjs/loader:1095:14) {
  code: 'ERR_INVALID_ARG_VALUE'
}

Additional information

Tested on both node version v16.4.0 and v15.9.0 (v15.9.0 was when keyObject.export() first started to support "jwk" as format.

Metadata

Metadata

Assignees

Labels

confirmed-bugIssues with confirmed bugs.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