Skip to content

Commit 7c9fedf

Browse files
committed
fixup! crypto: support JWK objects in create*Key
1 parent 9d87a40 commit 7c9fedf

File tree

3 files changed

+8
-10
lines changed

3 files changed

+8
-10
lines changed

doc/api/crypto.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -2527,7 +2527,7 @@ changes:
25272527
2 ** 32 - 1 bytes.
25282528
-->
25292529

2530-
* `key` {string|ArrayBuffer|Buffer|TypedArray|DataView|Object}
2530+
* `key` {string|ArrayBuffer|Buffer|TypedArray|DataView}
25312531
* `encoding` {string} The string encoding when `key` is a string.
25322532
* Returns: {KeyObject}
25332533

lib/internal/crypto/keys.js

+6-8
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,11 @@ function getKeyTypes(allowKeyObject, bufferOnly = false) {
405405

406406
function getKeyObjectHandleFromJwk(key, ctx) {
407407
validateObject(key, 'key');
408+
key = { ...key };
408409
validateOneOf(
409410
key.kty, 'key.kty', ['RSA', 'EC', 'OKP']);
410-
411411
const isPublic = ctx === kConsumePublic || ctx === kCreatePublic;
412412

413-
key = { ...key };
414-
415413
if (key.kty === 'OKP') {
416414
validateString(key.crv, 'key.crv');
417415
validateOneOf(
@@ -431,17 +429,17 @@ function getKeyObjectHandleFromJwk(key, ctx) {
431429
case 'Ed25519':
432430
case 'X25519':
433431
if (keyData.byteLength !== 32) {
434-
throw new ERR_CRYPTO_INVALID_JWK('Invalid JWK data');
432+
throw new ERR_CRYPTO_INVALID_JWK();
435433
}
436434
break;
437435
case 'Ed448':
438436
if (keyData.byteLength !== 57) {
439-
throw new ERR_CRYPTO_INVALID_JWK('Invalid JWK data');
437+
throw new ERR_CRYPTO_INVALID_JWK();
440438
}
441439
break;
442440
case 'X448':
443441
if (keyData.byteLength !== 56) {
444-
throw new ERR_CRYPTO_INVALID_JWK('Invalid JWK data');
442+
throw new ERR_CRYPTO_INVALID_JWK();
445443
}
446444
break;
447445
}
@@ -478,7 +476,7 @@ function getKeyObjectHandleFromJwk(key, ctx) {
478476
const handle = new KeyObjectHandle();
479477
const type = handle.initJwk(key, key.crv);
480478
if (type === undefined)
481-
throw new ERR_CRYPTO_INVALID_JWK('Invalid JWK data');
479+
throw new ERR_CRYPTO_INVALID_JWK();
482480

483481
return handle;
484482
}
@@ -505,7 +503,7 @@ function getKeyObjectHandleFromJwk(key, ctx) {
505503
const handle = new KeyObjectHandle();
506504
const type = handle.initJwk(key);
507505
if (type === undefined)
508-
throw new ERR_CRYPTO_INVALID_JWK('Invalid JWK data');
506+
throw new ERR_CRYPTO_INVALID_JWK();
509507

510508
return handle;
511509
}

lib/internal/errors.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ E('ERR_CRYPTO_INCOMPATIBLE_KEY', 'Incompatible %s: %s', Error);
836836
E('ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS', 'The selected key encoding %s %s.',
837837
Error);
838838
E('ERR_CRYPTO_INVALID_DIGEST', 'Invalid digest: %s', TypeError);
839-
E('ERR_CRYPTO_INVALID_JWK', 'Invalid JWK: %s', TypeError);
839+
E('ERR_CRYPTO_INVALID_JWK', 'Invalid JWK data', TypeError);
840840
E('ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE',
841841
'Invalid key object type %s, expected %s.', TypeError);
842842
E('ERR_CRYPTO_INVALID_STATE', 'Invalid state for operation %s', Error);

0 commit comments

Comments
 (0)