crypto: refactor subtle methods to use synchronous import · nodejs/node@d944a87

@@ -378,7 +378,7 @@ async function deriveKey(

378378

}

379379380380

return ReflectApply(

381-

importKey,

381+

importKeySync,

382382

this,

383383

['raw-secret', bits, derivedKeyAlgorithm, extractable, keyUsages],

384384

);

@@ -734,40 +734,7 @@ function aliasKeyFormat(format) {

734734

}

735735

}

736736737-

async function importKey(

738-

format,

739-

keyData,

740-

algorithm,

741-

extractable,

742-

keyUsages) {

743-

if (this !== subtle) throw new ERR_INVALID_THIS('SubtleCrypto');

744-745-

webidl ??= require('internal/crypto/webidl');

746-

const prefix = "Failed to execute 'importKey' on 'SubtleCrypto'";

747-

webidl.requiredArguments(arguments.length, 4, { prefix });

748-

format = webidl.converters.KeyFormat(format, {

749-

prefix,

750-

context: '1st argument',

751-

});

752-

const type = format === 'jwk' ? 'JsonWebKey' : 'BufferSource';

753-

keyData = webidl.converters[type](keyData, {

754-

prefix,

755-

context: '2nd argument',

756-

});

757-

algorithm = webidl.converters.AlgorithmIdentifier(algorithm, {

758-

prefix,

759-

context: '3rd argument',

760-

});

761-

extractable = webidl.converters.boolean(extractable, {

762-

prefix,

763-

context: '4th argument',

764-

});

765-

keyUsages = webidl.converters['sequence<KeyUsage>'](keyUsages, {

766-

prefix,

767-

context: '5th argument',

768-

});

769-770-

algorithm = normalizeAlgorithm(algorithm, 'importKey');

737+

function importKeySync(format, keyData, algorithm, extractable, keyUsages) {

771738

let result;

772739

switch (algorithm.name) {

773740

case 'RSASSA-PKCS1-v1_5':

@@ -879,6 +846,48 @@ async function importKey(

879846

return result;

880847

}

881848849+

async function importKey(

850+

format,

851+

keyData,

852+

algorithm,

853+

extractable,

854+

keyUsages) {

855+

if (this !== subtle) throw new ERR_INVALID_THIS('SubtleCrypto');

856+857+

webidl ??= require('internal/crypto/webidl');

858+

const prefix = "Failed to execute 'importKey' on 'SubtleCrypto'";

859+

webidl.requiredArguments(arguments.length, 4, { prefix });

860+

format = webidl.converters.KeyFormat(format, {

861+

prefix,

862+

context: '1st argument',

863+

});

864+

const type = format === 'jwk' ? 'JsonWebKey' : 'BufferSource';

865+

keyData = webidl.converters[type](keyData, {

866+

prefix,

867+

context: '2nd argument',

868+

});

869+

algorithm = webidl.converters.AlgorithmIdentifier(algorithm, {

870+

prefix,

871+

context: '3rd argument',

872+

});

873+

extractable = webidl.converters.boolean(extractable, {

874+

prefix,

875+

context: '4th argument',

876+

});

877+

keyUsages = webidl.converters['sequence<KeyUsage>'](keyUsages, {

878+

prefix,

879+

context: '5th argument',

880+

});

881+882+

algorithm = normalizeAlgorithm(algorithm, 'importKey');

883+884+

return ReflectApply(

885+

importKeySync,

886+

this,

887+

[format, keyData, algorithm, extractable, keyUsages],

888+

);

889+

}

890+882891

// subtle.wrapKey() is essentially a subtle.exportKey() followed

883892

// by a subtle.encrypt().

884893

async function wrapKey(format, key, wrappingKey, algorithm) {

@@ -985,6 +994,8 @@ async function unwrapKey(

985994

unwrapAlgo = normalizeAlgorithm(unwrapAlgo, 'decrypt');

986995

}

987996997+

unwrappedKeyAlgo = normalizeAlgorithm(unwrappedKeyAlgo, 'importKey');

998+988999

let keyData = await cipherOrWrap(

9891000

kWebCryptoCipherDecrypt,

9901001

unwrapAlgo,

@@ -1005,7 +1016,7 @@ async function unwrapKey(

10051016

}

1006101710071018

return ReflectApply(

1008-

importKey,

1019+

importKeySync,

10091020

this,

10101021

[format, keyData, unwrappedKeyAlgo, extractable, keyUsages],

10111022

);

@@ -1318,8 +1329,8 @@ async function encapsulateKey(encapsulationAlgorithm, encapsulationKey, sharedKe

13181329

throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError');

13191330

}

132013311321-

const sharedKey = await ReflectApply(

1322-

importKey,

1332+

const sharedKey = ReflectApply(

1333+

importKeySync,

13231334

this,

13241335

['raw-secret', encapsulateBits.sharedKey, normalizedSharedKeyAlgorithm, extractable, usages],

13251336

);

@@ -1439,7 +1450,7 @@ async function decapsulateKey(

14391450

}

1440145114411452

return ReflectApply(

1442-

importKey,

1453+

importKeySync,

14431454

this,

14441455

['raw-secret', decapsulatedBits, normalizedSharedKeyAlgorithm, extractable, usages],

14451456

);