Skip to content

Commit bb2857b

Browse files
panvasxa
authored andcommitted
crypto: align key argument names in docs and error messages
Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #62527 Backport-PR-URL: #63563 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b9d5e87 commit bb2857b

7 files changed

Lines changed: 158 additions & 16 deletions

File tree

doc/api/crypto.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2734,14 +2734,14 @@ encoding of `'utf8'` is enforced. If `data` is a [`Buffer`][], `TypedArray`, or
27342734

27352735
This can be called many times with new data as it is streamed.
27362736

2737-
### `verify.verify(object, signature[, signatureEncoding])`
2737+
### `verify.verify(key, signature[, signatureEncoding])`
27382738

27392739
<!-- YAML
27402740
added: v0.1.92
27412741
changes:
27422742
- version: v15.0.0
27432743
pr-url: https://github.com/nodejs/node/pull/35093
2744-
description: The object can also be an ArrayBuffer and CryptoKey.
2744+
description: The key can also be an ArrayBuffer and CryptoKey.
27452745
- version:
27462746
- v13.2.0
27472747
- v12.16.0
@@ -2760,7 +2760,7 @@ changes:
27602760

27612761
<!--lint disable maximum-line-length remark-lint-->
27622762

2763-
* `object` {Object|string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey}
2763+
* `key` {Object|string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey}
27642764
* `dsaEncoding` {string}
27652765
* `padding` {integer}
27662766
* `saltLength` {integer}
@@ -2771,10 +2771,10 @@ changes:
27712771

27722772
<!--lint enable maximum-line-length remark-lint-->
27732773

2774-
Verifies the provided data using the given `object` and `signature`.
2774+
Verifies the provided data using the given `key` and `signature`.
27752775

2776-
If `object` is not a [`KeyObject`][], this function behaves as if
2777-
`object` had been passed to [`crypto.createPublicKey()`][]. If it is an
2776+
If `key` is not a [`KeyObject`][], this function behaves as if
2777+
`key` had been passed to [`crypto.createPublicKey()`][]. If it is an
27782778
object, the following additional properties can be passed:
27792779

27802780
* `dsaEncoding` {string} For DSA and ECDSA, this option specifies the
@@ -6933,7 +6933,7 @@ See the [list of SSL OP Flags][] for details.
69336933
[`stream.transform` options]: stream.md#new-streamtransformoptions
69346934
[`util.promisify()`]: util.md#utilpromisifyoriginal
69356935
[`verify.update()`]: #verifyupdatedata-inputencoding
6936-
[`verify.verify()`]: #verifyverifyobject-signature-signatureencoding
6936+
[`verify.verify()`]: #verifyverifykey-signature-signatureencoding
69376937
[`x509.fingerprint256`]: #x509fingerprint256
69386938
[`x509.verify(publicKey)`]: #x509verifypublickey
69396939
[argon2]: https://www.rfc-editor.org/rfc/rfc9106.html

doc/api/deprecations.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4484,7 +4484,7 @@ will throw an error in a future version.
44844484
[`Sign.prototype.sign()`]: crypto.md#signsignprivatekey-outputencoding
44854485
[`SlowBuffer`]: buffer.md#class-slowbuffer
44864486
[`String.prototype.toWellFormed`]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toWellFormed
4487-
[`Verify.prototype.verify()`]: crypto.md#verifyverifyobject-signature-signatureencoding
4487+
[`Verify.prototype.verify()`]: crypto.md#verifyverifykey-signature-signatureencoding
44884488
[`WriteStream.open()`]: fs.md#class-fswritestream
44894489
[`assert.CallTracker`]: assert.md#class-assertcalltracker
44904490
[`assert`]: assert.md

lib/internal/crypto/cipher.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,15 @@ const { normalizeEncoding } = require('internal/util');
6363
const { StringDecoder } = require('string_decoder');
6464

6565
function rsaFunctionFor(method, defaultPadding, keyType) {
66-
return (options, buffer) => {
66+
const keyName = keyType === 'private' ? 'privateKey' : undefined;
67+
return (key, buffer) => {
6768
const { format, type, data, passphrase, namedCurve } =
6869
keyType === 'private' ?
69-
preparePrivateKey(options) :
70-
preparePublicOrPrivateKey(options);
71-
const padding = options.padding || defaultPadding;
72-
const { oaepHash, encoding } = options;
73-
let { oaepLabel } = options;
70+
preparePrivateKey(key, keyName) :
71+
preparePublicOrPrivateKey(key, keyName);
72+
const padding = key.padding || defaultPadding;
73+
const { oaepHash, encoding } = key;
74+
let { oaepLabel } = key;
7475
if (oaepHash !== undefined)
7576
validateString(oaepHash, 'key.oaepHash');
7677
if (oaepLabel !== undefined)

lib/internal/crypto/keygen.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function parseKeyEncoding(keyType, options = kEmptyObject) {
148148
format: publicFormat,
149149
type: publicType,
150150
} = parsePublicKeyEncoding(publicKeyEncoding, keyType,
151-
'publicKeyEncoding'));
151+
'options.publicKeyEncoding'));
152152
} else {
153153
throw new ERR_INVALID_ARG_VALUE('options.publicKeyEncoding',
154154
publicKeyEncoding);
@@ -164,7 +164,7 @@ function parseKeyEncoding(keyType, options = kEmptyObject) {
164164
cipher,
165165
passphrase,
166166
} = parsePrivateKeyEncoding(privateKeyEncoding, keyType,
167-
'privateKeyEncoding'));
167+
'options.privateKeyEncoding'));
168168
} else {
169169
throw new ERR_INVALID_ARG_VALUE('options.privateKeyEncoding',
170170
privateKeyEncoding);

test/parallel/test-crypto-dh-stateless.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,49 @@ for (const { privateKey: alicePriv, publicKey: bobPub } of [
398398
}
399399
}
400400

401+
// Test that error messages include the correct property path
402+
{
403+
const kp = crypto.generateKeyPairSync('x25519');
404+
const pub = kp.publicKey.export({ type: 'spki', format: 'pem' });
405+
const priv = kp.privateKey.export({ type: 'pkcs8', format: 'pem' });
406+
407+
// Invalid privateKey format
408+
assert.throws(() => crypto.diffieHellman({
409+
privateKey: { key: Buffer.alloc(0), format: 'banana', type: 'pkcs8' },
410+
publicKey: pub,
411+
}), {
412+
code: 'ERR_INVALID_ARG_VALUE',
413+
message: /options\.privateKey\.format/,
414+
});
415+
416+
// Invalid privateKey type
417+
assert.throws(() => crypto.diffieHellman({
418+
privateKey: { key: Buffer.alloc(0), format: 'der', type: 'banana' },
419+
publicKey: pub,
420+
}), {
421+
code: 'ERR_INVALID_ARG_VALUE',
422+
message: /options\.privateKey\.type/,
423+
});
424+
425+
// Invalid publicKey format
426+
assert.throws(() => crypto.diffieHellman({
427+
publicKey: { key: Buffer.alloc(0), format: 'banana', type: 'spki' },
428+
privateKey: priv,
429+
}), {
430+
code: 'ERR_INVALID_ARG_VALUE',
431+
message: /options\.publicKey\.format/,
432+
});
433+
434+
// Invalid publicKey type
435+
assert.throws(() => crypto.diffieHellman({
436+
publicKey: { key: Buffer.alloc(0), format: 'der', type: 'banana' },
437+
privateKey: priv,
438+
}), {
439+
code: 'ERR_INVALID_ARG_VALUE',
440+
message: /options\.publicKey\.type/,
441+
});
442+
}
443+
401444
// Test C++ error conditions
402445
{
403446
const ec256 = crypto.generateKeyPairSync('ec', { namedCurve: 'P-256' });

test/parallel/test-crypto-key-objects.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,3 +1085,38 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
10851085
}, { code: 'ERR_INVALID_ARG_TYPE', message: /The "key\.key" property must be of type object/ });
10861086
}
10871087
}
1088+
1089+
// Test that createPublicKey/createPrivateKey error messages use 'key.<property>' paths
1090+
{
1091+
// createPrivateKey with invalid format
1092+
assert.throws(() => {
1093+
createPrivateKey({ key: Buffer.alloc(0), format: 'banana', type: 'pkcs8' });
1094+
}, {
1095+
code: 'ERR_INVALID_ARG_VALUE',
1096+
message: /key\.format/,
1097+
});
1098+
1099+
// createPrivateKey with invalid type
1100+
assert.throws(() => {
1101+
createPrivateKey({ key: Buffer.alloc(0), format: 'der', type: 'banana' });
1102+
}, {
1103+
code: 'ERR_INVALID_ARG_VALUE',
1104+
message: /key\.type/,
1105+
});
1106+
1107+
// createPublicKey with invalid format
1108+
assert.throws(() => {
1109+
createPublicKey({ key: Buffer.alloc(0), format: 'banana', type: 'spki' });
1110+
}, {
1111+
code: 'ERR_INVALID_ARG_VALUE',
1112+
message: /key\.format/,
1113+
});
1114+
1115+
// createPublicKey with invalid type
1116+
assert.throws(() => {
1117+
createPublicKey({ key: Buffer.alloc(0), format: 'der', type: 'banana' });
1118+
}, {
1119+
code: 'ERR_INVALID_ARG_VALUE',
1120+
message: /key\.type/,
1121+
});
1122+
}

test/parallel/test-crypto-sign-verify.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,3 +1031,66 @@ if (hasOpenSSL(3, 2)) {
10311031
assert.strictEqual(crypto.verify('SHA256', dataBuffer, publicKey, sigSAB), true);
10321032
}
10331033
}
1034+
1035+
// Test that sign/verify error messages use correct property paths
1036+
{
1037+
// Sign with invalid format
1038+
assert.throws(() => {
1039+
crypto.createSign('SHA256').update('test').sign({
1040+
key: Buffer.alloc(0), format: 'banana', type: 'pkcs8',
1041+
});
1042+
}, {
1043+
code: 'ERR_INVALID_ARG_VALUE',
1044+
message: /privateKey\.format/,
1045+
});
1046+
1047+
// Sign with invalid type
1048+
assert.throws(() => {
1049+
crypto.createSign('SHA256').update('test').sign({
1050+
key: Buffer.alloc(0), format: 'der', type: 'banana',
1051+
});
1052+
}, {
1053+
code: 'ERR_INVALID_ARG_VALUE',
1054+
message: /privateKey\.type/,
1055+
});
1056+
1057+
// Verify with invalid format
1058+
assert.throws(() => {
1059+
crypto.createVerify('SHA256').update('test').verify({
1060+
key: Buffer.alloc(0), format: 'banana', type: 'spki',
1061+
}, Buffer.alloc(0));
1062+
}, {
1063+
code: 'ERR_INVALID_ARG_VALUE',
1064+
message: /key\.format/,
1065+
});
1066+
1067+
// Verify with invalid type
1068+
assert.throws(() => {
1069+
crypto.createVerify('SHA256').update('test').verify({
1070+
key: Buffer.alloc(0), format: 'der', type: 'banana',
1071+
}, Buffer.alloc(0));
1072+
}, {
1073+
code: 'ERR_INVALID_ARG_VALUE',
1074+
message: /key\.type/,
1075+
});
1076+
1077+
// crypto.sign with invalid format
1078+
assert.throws(() => {
1079+
crypto.sign('SHA256', Buffer.from('test'), {
1080+
key: Buffer.alloc(0), format: 'banana', type: 'pkcs8',
1081+
});
1082+
}, {
1083+
code: 'ERR_INVALID_ARG_VALUE',
1084+
message: /key\.format/,
1085+
});
1086+
1087+
// crypto.verify with invalid format
1088+
assert.throws(() => {
1089+
crypto.verify('SHA256', Buffer.from('test'), {
1090+
key: Buffer.alloc(0), format: 'banana', type: 'spki',
1091+
}, Buffer.alloc(0));
1092+
}, {
1093+
code: 'ERR_INVALID_ARG_VALUE',
1094+
message: /key\.format/,
1095+
});
1096+
}

0 commit comments

Comments
 (0)