Skip to content

Commit

Permalink
fixup! crypto: support JWK objects in create*Key
Browse files Browse the repository at this point in the history
  • Loading branch information
panva committed Feb 18, 2021
1 parent 9d87a40 commit 7c9fedf
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 10 deletions.
2 changes: 1 addition & 1 deletion doc/api/crypto.md
Original file line number Diff line number Diff line change
Expand Up @@ -2527,7 +2527,7 @@ changes:
2 ** 32 - 1 bytes.
-->

* `key` {string|ArrayBuffer|Buffer|TypedArray|DataView|Object}
* `key` {string|ArrayBuffer|Buffer|TypedArray|DataView}
* `encoding` {string} The string encoding when `key` is a string.
* Returns: {KeyObject}

Expand Down
14 changes: 6 additions & 8 deletions lib/internal/crypto/keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,11 @@ function getKeyTypes(allowKeyObject, bufferOnly = false) {

function getKeyObjectHandleFromJwk(key, ctx) {
validateObject(key, 'key');
key = { ...key };
validateOneOf(
key.kty, 'key.kty', ['RSA', 'EC', 'OKP']);

const isPublic = ctx === kConsumePublic || ctx === kCreatePublic;

key = { ...key };

if (key.kty === 'OKP') {
validateString(key.crv, 'key.crv');
validateOneOf(
Expand All @@ -431,17 +429,17 @@ function getKeyObjectHandleFromJwk(key, ctx) {
case 'Ed25519':
case 'X25519':
if (keyData.byteLength !== 32) {
throw new ERR_CRYPTO_INVALID_JWK('Invalid JWK data');
throw new ERR_CRYPTO_INVALID_JWK();
}
break;
case 'Ed448':
if (keyData.byteLength !== 57) {
throw new ERR_CRYPTO_INVALID_JWK('Invalid JWK data');
throw new ERR_CRYPTO_INVALID_JWK();
}
break;
case 'X448':
if (keyData.byteLength !== 56) {
throw new ERR_CRYPTO_INVALID_JWK('Invalid JWK data');
throw new ERR_CRYPTO_INVALID_JWK();
}
break;
}
Expand Down Expand Up @@ -478,7 +476,7 @@ function getKeyObjectHandleFromJwk(key, ctx) {
const handle = new KeyObjectHandle();
const type = handle.initJwk(key, key.crv);
if (type === undefined)
throw new ERR_CRYPTO_INVALID_JWK('Invalid JWK data');
throw new ERR_CRYPTO_INVALID_JWK();

return handle;
}
Expand All @@ -505,7 +503,7 @@ function getKeyObjectHandleFromJwk(key, ctx) {
const handle = new KeyObjectHandle();
const type = handle.initJwk(key);
if (type === undefined)
throw new ERR_CRYPTO_INVALID_JWK('Invalid JWK data');
throw new ERR_CRYPTO_INVALID_JWK();

return handle;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -836,7 +836,7 @@ E('ERR_CRYPTO_INCOMPATIBLE_KEY', 'Incompatible %s: %s', Error);
E('ERR_CRYPTO_INCOMPATIBLE_KEY_OPTIONS', 'The selected key encoding %s %s.',
Error);
E('ERR_CRYPTO_INVALID_DIGEST', 'Invalid digest: %s', TypeError);
E('ERR_CRYPTO_INVALID_JWK', 'Invalid JWK: %s', TypeError);
E('ERR_CRYPTO_INVALID_JWK', 'Invalid JWK data', TypeError);
E('ERR_CRYPTO_INVALID_KEY_OBJECT_TYPE',
'Invalid key object type %s, expected %s.', TypeError);
E('ERR_CRYPTO_INVALID_STATE', 'Invalid state for operation %s', Error);
Expand Down

0 comments on commit 7c9fedf

Please sign in to comment.