Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d4cec26

Browse filesBrowse files
panvaaduh95
authored andcommitted
crypto: align key argument names in docs and error messages
Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #62527 Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 073e84d commit d4cec26
Copy full SHA for d4cec26

8 files changed

+175-33Lines changed: 175 additions & 33 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎doc/api/crypto.md‎

Copy file name to clipboardExpand all lines: doc/api/crypto.md
+7-7Lines changed: 7 additions & 7 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -2743,14 +2743,14 @@ encoding of `'utf8'` is enforced. If `data` is a [`Buffer`][], `TypedArray`, or
27432743

27442744
This can be called many times with new data as it is streamed.
27452745

2746-
### `verify.verify(object, signature[, signatureEncoding])`
2746+
### `verify.verify(key, signature[, signatureEncoding])`
27472747

27482748
<!-- YAML
27492749
added: v0.1.92
27502750
changes:
27512751
- version: v15.0.0
27522752
pr-url: https://github.com/nodejs/node/pull/35093
2753-
description: The object can also be an ArrayBuffer and CryptoKey.
2753+
description: The key can also be an ArrayBuffer and CryptoKey.
27542754
- version:
27552755
- v13.2.0
27562756
- v12.16.0
@@ -2769,7 +2769,7 @@ changes:
27692769

27702770
<!--lint disable maximum-line-length remark-lint-->
27712771

2772-
* `object` {Object|string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey}
2772+
* `key` {Object|string|ArrayBuffer|Buffer|TypedArray|DataView|KeyObject|CryptoKey}
27732773
* `dsaEncoding` {string}
27742774
* `padding` {integer}
27752775
* `saltLength` {integer}
@@ -2780,10 +2780,10 @@ changes:
27802780

27812781
<!--lint enable maximum-line-length remark-lint-->
27822782

2783-
Verifies the provided data using the given `object` and `signature`.
2783+
Verifies the provided data using the given `key` and `signature`.
27842784

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

27892789
* `dsaEncoding` {string} For DSA and ECDSA, this option specifies the
@@ -6952,7 +6952,7 @@ See the [list of SSL OP Flags][] for details.
69526952
[`stream.transform` options]: stream.md#new-streamtransformoptions
69536953
[`util.promisify()`]: util.md#utilpromisifyoriginal
69546954
[`verify.update()`]: #verifyupdatedata-inputencoding
6955-
[`verify.verify()`]: #verifyverifyobject-signature-signatureencoding
6955+
[`verify.verify()`]: #verifyverifykey-signature-signatureencoding
69566956
[`x509.fingerprint256`]: #x509fingerprint256
69576957
[`x509.verify(publicKey)`]: #x509verifypublickey
69586958
[argon2]: https://www.rfc-editor.org/rfc/rfc9106.html
Collapse file

‎lib/internal/crypto/cipher.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/cipher.js
+7-6Lines 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)
Collapse file

‎lib/internal/crypto/keygen.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/keygen.js
+2-2Lines 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);
Collapse file

‎lib/internal/crypto/keys.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/keys.js
+5-5Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -466,9 +466,9 @@ function parseKeyType(typeStr, required, keyType, isPublic, optionName) {
466466
throw new ERR_INVALID_ARG_VALUE(optionName, typeStr);
467467
}
468468

469-
function option(name, objName) {
470-
return objName === undefined ?
471-
`options.${name}` : `options.${objName}.${name}`;
469+
function option(name, prefix) {
470+
return prefix === undefined ?
471+
`options.${name}` : `${prefix}.${name}`;
472472
}
473473

474474
function parseKeyFormatAndType(enc, keyType, isPublic, objName) {
@@ -668,7 +668,7 @@ function prepareAsymmetricKey(key, ctx, name = 'key') {
668668
if (key.asymmetricKeyType === 'ec') {
669669
validateString(key.namedCurve, `${name}.namedCurve`);
670670
}
671-
const rawFormat = parseKeyFormat(format, undefined, 'options.format');
671+
const rawFormat = parseKeyFormat(format, undefined, `${name}.format`);
672672
return {
673673
data: getArrayBufferOrView(data, `${name}.key`),
674674
format: rawFormat,
@@ -689,7 +689,7 @@ function prepareAsymmetricKey(key, ctx, name = 'key') {
689689
(ctx === kConsumePrivate || ctx === kCreatePrivate) ? false : undefined;
690690
return {
691691
data: getArrayBufferOrView(data, `${name}.key`, encoding),
692-
...parseKeyEncoding(key, undefined, isPublic),
692+
...parseKeyEncoding(key, undefined, isPublic, name),
693693
};
694694
}
695695

Collapse file

‎lib/internal/crypto/sig.js‎

Copy file name to clipboardExpand all lines: lib/internal/crypto/sig.js
+11-11Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,19 @@ function getIntOption(name, options) {
130130
return undefined;
131131
}
132132

133-
Sign.prototype.sign = function sign(options, encoding) {
134-
if (!options)
133+
Sign.prototype.sign = function sign(privateKey, encoding) {
134+
if (!privateKey)
135135
throw new ERR_CRYPTO_SIGN_KEY_REQUIRED();
136136

137137
const { data, format, type, passphrase, namedCurve } =
138-
preparePrivateKey(options);
138+
preparePrivateKey(privateKey, 'privateKey');
139139

140140
// Options specific to RSA
141-
const rsaPadding = getPadding(options);
142-
const pssSaltLength = getSaltLength(options);
141+
const rsaPadding = getPadding(privateKey);
142+
const pssSaltLength = getSaltLength(privateKey);
143143

144144
// Options specific to (EC)DSA
145-
const dsaSigEnc = getDSASignatureEncoding(options);
145+
const dsaSigEnc = getDSASignatureEncoding(privateKey);
146146

147147
const ret = this[kHandle].sign(data, format, type,
148148
passphrase, namedCurve,
@@ -232,21 +232,21 @@ ObjectSetPrototypeOf(Verify, Writable);
232232
Verify.prototype._write = Sign.prototype._write;
233233
Verify.prototype.update = Sign.prototype.update;
234234

235-
Verify.prototype.verify = function verify(options, signature, sigEncoding) {
235+
Verify.prototype.verify = function verify(key, signature, sigEncoding) {
236236
const {
237237
data,
238238
format,
239239
type,
240240
passphrase,
241241
namedCurve,
242-
} = preparePublicOrPrivateKey(options);
242+
} = preparePublicOrPrivateKey(key, 'key');
243243

244244
// Options specific to RSA
245-
const rsaPadding = getPadding(options);
246-
const pssSaltLength = getSaltLength(options);
245+
const rsaPadding = getPadding(key);
246+
const pssSaltLength = getSaltLength(key);
247247

248248
// Options specific to (EC)DSA
249-
const dsaSigEnc = getDSASignatureEncoding(options);
249+
const dsaSigEnc = getDSASignatureEncoding(key);
250250

251251
signature = getArrayBufferOrView(signature, 'signature', sigEncoding);
252252

Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-crypto-dh-stateless.js
+43Lines 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' });
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-crypto-key-objects.js
+36-1Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
343343
createPrivateKey({ key: Buffer.alloc(0), format: 'der', type: 'spki' });
344344
}, {
345345
code: 'ERR_INVALID_ARG_VALUE',
346-
message: "The property 'options.type' is invalid. Received 'spki'"
346+
message: "The property 'key.type' is invalid. Received 'spki'"
347347
});
348348

349349
// Unlike SPKI, PKCS#1 is a valid encoding for private keys (and public keys),
@@ -1074,3 +1074,38 @@ const privateDsa = fixtures.readKey('dsa_private_encrypted_1025.pem',
10741074
}, { code: 'ERR_INVALID_ARG_TYPE', message: /The "key\.key" property must be of type object/ });
10751075
}
10761076
}
1077+
1078+
// Test that createPublicKey/createPrivateKey error messages use 'key.<property>' paths
1079+
{
1080+
// createPrivateKey with invalid format
1081+
assert.throws(() => {
1082+
createPrivateKey({ key: Buffer.alloc(0), format: 'banana', type: 'pkcs8' });
1083+
}, {
1084+
code: 'ERR_INVALID_ARG_VALUE',
1085+
message: /key\.format/,
1086+
});
1087+
1088+
// createPrivateKey with invalid type
1089+
assert.throws(() => {
1090+
createPrivateKey({ key: Buffer.alloc(0), format: 'der', type: 'banana' });
1091+
}, {
1092+
code: 'ERR_INVALID_ARG_VALUE',
1093+
message: /key\.type/,
1094+
});
1095+
1096+
// createPublicKey with invalid format
1097+
assert.throws(() => {
1098+
createPublicKey({ key: Buffer.alloc(0), format: 'banana', type: 'spki' });
1099+
}, {
1100+
code: 'ERR_INVALID_ARG_VALUE',
1101+
message: /key\.format/,
1102+
});
1103+
1104+
// createPublicKey with invalid type
1105+
assert.throws(() => {
1106+
createPublicKey({ key: Buffer.alloc(0), format: 'der', type: 'banana' });
1107+
}, {
1108+
code: 'ERR_INVALID_ARG_VALUE',
1109+
message: /key\.type/,
1110+
});
1111+
}
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-crypto-sign-verify.js
+64-1Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -891,7 +891,7 @@ if (hasOpenSSL(3, 2)) {
891891
}, { code: 'ERR_INVALID_ARG_TYPE', message: /The "key\.key" property must be of type object/ });
892892
assert.throws(() => {
893893
crypto.createSign('sha256').sign({ key, format: 'jwk' });
894-
}, { code: 'ERR_INVALID_ARG_TYPE', message: /The "key\.key" property must be of type object/ });
894+
}, { code: 'ERR_INVALID_ARG_TYPE', message: /The "privateKey\.key" property must be of type object/ });
895895
}
896896
}
897897

@@ -932,3 +932,66 @@ if (hasOpenSSL(3, 2)) {
932932
}, { code: 'ERR_OSSL_EVP_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE', message: /operation not supported for this keytype/ });
933933
}
934934
}
935+
936+
// Test that sign/verify error messages use correct property paths
937+
{
938+
// Sign with invalid format
939+
assert.throws(() => {
940+
crypto.createSign('SHA256').update('test').sign({
941+
key: Buffer.alloc(0), format: 'banana', type: 'pkcs8',
942+
});
943+
}, {
944+
code: 'ERR_INVALID_ARG_VALUE',
945+
message: /privateKey\.format/,
946+
});
947+
948+
// Sign with invalid type
949+
assert.throws(() => {
950+
crypto.createSign('SHA256').update('test').sign({
951+
key: Buffer.alloc(0), format: 'der', type: 'banana',
952+
});
953+
}, {
954+
code: 'ERR_INVALID_ARG_VALUE',
955+
message: /privateKey\.type/,
956+
});
957+
958+
// Verify with invalid format
959+
assert.throws(() => {
960+
crypto.createVerify('SHA256').update('test').verify({
961+
key: Buffer.alloc(0), format: 'banana', type: 'spki',
962+
}, Buffer.alloc(0));
963+
}, {
964+
code: 'ERR_INVALID_ARG_VALUE',
965+
message: /key\.format/,
966+
});
967+
968+
// Verify with invalid type
969+
assert.throws(() => {
970+
crypto.createVerify('SHA256').update('test').verify({
971+
key: Buffer.alloc(0), format: 'der', type: 'banana',
972+
}, Buffer.alloc(0));
973+
}, {
974+
code: 'ERR_INVALID_ARG_VALUE',
975+
message: /key\.type/,
976+
});
977+
978+
// crypto.sign with invalid format
979+
assert.throws(() => {
980+
crypto.sign('SHA256', Buffer.from('test'), {
981+
key: Buffer.alloc(0), format: 'banana', type: 'pkcs8',
982+
});
983+
}, {
984+
code: 'ERR_INVALID_ARG_VALUE',
985+
message: /key\.format/,
986+
});
987+
988+
// crypto.verify with invalid format
989+
assert.throws(() => {
990+
crypto.verify('SHA256', Buffer.from('test'), {
991+
key: Buffer.alloc(0), format: 'banana', type: 'spki',
992+
}, Buffer.alloc(0));
993+
}, {
994+
code: 'ERR_INVALID_ARG_VALUE',
995+
message: /key\.format/,
996+
});
997+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.