Skip to content

Commit 2fa5e50

Browse files
codebyteretargos
authored andcommitted
crypto: handle initEDRaw pkey failure
PR-URL: #40188 Reviewed-By: Filip Skokan <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent bddf8c2 commit 2fa5e50

File tree

2 files changed

+8
-15
lines changed

2 files changed

+8
-15
lines changed

lib/internal/crypto/ec.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,12 @@ function createECRawKey(namedCurve, keyData, isPublic) {
122122
break;
123123
}
124124

125-
if (isPublic) {
126-
handle.initEDRaw(namedCurve, keyData, kKeyTypePublic);
127-
return new PublicKeyObject(handle);
125+
const keyType = isPublic ? kKeyTypePublic : kKeyTypePrivate;
126+
if (!handle.initEDRaw(namedCurve, keyData, keyType)) {
127+
throw lazyDOMException('Failure to generate key object');
128128
}
129129

130-
handle.initEDRaw(namedCurve, keyData, kKeyTypePrivate);
131-
return new PrivateKeyObject(handle);
130+
return isPublic ? new PublicKeyObject(handle) : new PrivateKeyObject(handle);
132131
}
133132

134133
async function ecGenerateKey(algorithm, extractable, keyUsages) {

lib/internal/crypto/keys.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -435,16 +435,10 @@ function getKeyObjectHandleFromJwk(key, ctx) {
435435
}
436436

437437
const handle = new KeyObjectHandle();
438-
if (isPublic) {
439-
handle.initEDRaw(
440-
`NODE-${key.crv.toUpperCase()}`,
441-
keyData,
442-
kKeyTypePublic);
443-
} else {
444-
handle.initEDRaw(
445-
`NODE-${key.crv.toUpperCase()}`,
446-
keyData,
447-
kKeyTypePrivate);
438+
439+
const keyType = isPublic ? kKeyTypePublic : kKeyTypePrivate;
440+
if (!handle.initEDRaw(`NODE-${key.crv.toUpperCase()}`, keyData, keyType)) {
441+
throw new ERR_CRYPTO_INVALID_JWK();
448442
}
449443

450444
return handle;

0 commit comments

Comments
 (0)