From 2fa5e5011f686ca47a9b27450d414a81b2190490 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Thu, 23 Sep 2021 13:43:01 +0200 Subject: [PATCH] crypto: handle initEDRaw pkey failure PR-URL: https://github.com/nodejs/node/pull/40188 Reviewed-By: Filip Skokan Reviewed-By: James M Snell --- lib/internal/crypto/ec.js | 9 ++++----- lib/internal/crypto/keys.js | 14 ++++---------- 2 files changed, 8 insertions(+), 15 deletions(-) diff --git a/lib/internal/crypto/ec.js b/lib/internal/crypto/ec.js index 76c6b306cf25a3..c684d6c66af26a 100644 --- a/lib/internal/crypto/ec.js +++ b/lib/internal/crypto/ec.js @@ -122,13 +122,12 @@ function createECRawKey(namedCurve, keyData, isPublic) { break; } - if (isPublic) { - handle.initEDRaw(namedCurve, keyData, kKeyTypePublic); - return new PublicKeyObject(handle); + const keyType = isPublic ? kKeyTypePublic : kKeyTypePrivate; + if (!handle.initEDRaw(namedCurve, keyData, keyType)) { + throw lazyDOMException('Failure to generate key object'); } - handle.initEDRaw(namedCurve, keyData, kKeyTypePrivate); - return new PrivateKeyObject(handle); + return isPublic ? new PublicKeyObject(handle) : new PrivateKeyObject(handle); } async function ecGenerateKey(algorithm, extractable, keyUsages) { diff --git a/lib/internal/crypto/keys.js b/lib/internal/crypto/keys.js index ce053fbb4b800a..ea2ecc262ed665 100644 --- a/lib/internal/crypto/keys.js +++ b/lib/internal/crypto/keys.js @@ -435,16 +435,10 @@ function getKeyObjectHandleFromJwk(key, ctx) { } const handle = new KeyObjectHandle(); - if (isPublic) { - handle.initEDRaw( - `NODE-${key.crv.toUpperCase()}`, - keyData, - kKeyTypePublic); - } else { - handle.initEDRaw( - `NODE-${key.crv.toUpperCase()}`, - keyData, - kKeyTypePrivate); + + const keyType = isPublic ? kKeyTypePublic : kKeyTypePrivate; + if (!handle.initEDRaw(`NODE-${key.crv.toUpperCase()}`, keyData, keyType)) { + throw new ERR_CRYPTO_INVALID_JWK(); } return handle;