From 9d47acea62e19c4b1296305f02e38ec07f9464ec Mon Sep 17 00:00:00 2001 From: Filip Skokan Date: Fri, 5 Sep 2025 17:12:02 +0200 Subject: [PATCH] crypto: refactor subtle methods to use synchronous import Refs: #59699 --- lib/internal/crypto/webcrypto.js | 89 ++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 39 deletions(-) diff --git a/lib/internal/crypto/webcrypto.js b/lib/internal/crypto/webcrypto.js index 18b5253e2aa463..ad2791203772d9 100644 --- a/lib/internal/crypto/webcrypto.js +++ b/lib/internal/crypto/webcrypto.js @@ -363,7 +363,7 @@ async function deriveKey( } return ReflectApply( - importKey, + importKeySync, this, ['raw-secret', bits, derivedKeyAlgorithm, extractable, keyUsages], ); @@ -708,40 +708,7 @@ function aliasKeyFormat(format) { } } -async function importKey( - format, - keyData, - algorithm, - extractable, - keyUsages) { - if (this !== subtle) throw new ERR_INVALID_THIS('SubtleCrypto'); - - webidl ??= require('internal/crypto/webidl'); - const prefix = "Failed to execute 'importKey' on 'SubtleCrypto'"; - webidl.requiredArguments(arguments.length, 4, { prefix }); - format = webidl.converters.KeyFormat(format, { - prefix, - context: '1st argument', - }); - const type = format === 'jwk' ? 'JsonWebKey' : 'BufferSource'; - keyData = webidl.converters[type](keyData, { - prefix, - context: '2nd argument', - }); - algorithm = webidl.converters.AlgorithmIdentifier(algorithm, { - prefix, - context: '3rd argument', - }); - extractable = webidl.converters.boolean(extractable, { - prefix, - context: '4th argument', - }); - keyUsages = webidl.converters['sequence'](keyUsages, { - prefix, - context: '5th argument', - }); - - algorithm = normalizeAlgorithm(algorithm, 'importKey'); +function importKeySync(format, keyData, algorithm, extractable, keyUsages) { let result; switch (algorithm.name) { case 'RSASSA-PKCS1-v1_5': @@ -853,6 +820,48 @@ async function importKey( return result; } +async function importKey( + format, + keyData, + algorithm, + extractable, + keyUsages) { + if (this !== subtle) throw new ERR_INVALID_THIS('SubtleCrypto'); + + webidl ??= require('internal/crypto/webidl'); + const prefix = "Failed to execute 'importKey' on 'SubtleCrypto'"; + webidl.requiredArguments(arguments.length, 4, { prefix }); + format = webidl.converters.KeyFormat(format, { + prefix, + context: '1st argument', + }); + const type = format === 'jwk' ? 'JsonWebKey' : 'BufferSource'; + keyData = webidl.converters[type](keyData, { + prefix, + context: '2nd argument', + }); + algorithm = webidl.converters.AlgorithmIdentifier(algorithm, { + prefix, + context: '3rd argument', + }); + extractable = webidl.converters.boolean(extractable, { + prefix, + context: '4th argument', + }); + keyUsages = webidl.converters['sequence'](keyUsages, { + prefix, + context: '5th argument', + }); + + algorithm = normalizeAlgorithm(algorithm, 'importKey'); + + return ReflectApply( + importKeySync, + this, + [format, keyData, algorithm, extractable, keyUsages], + ); +} + // subtle.wrapKey() is essentially a subtle.exportKey() followed // by a subtle.encrypt(). async function wrapKey(format, key, wrappingKey, algorithm) { @@ -959,6 +968,8 @@ async function unwrapKey( unwrapAlgo = normalizeAlgorithm(unwrapAlgo, 'decrypt'); } + unwrappedKeyAlgo = normalizeAlgorithm(unwrappedKeyAlgo, 'importKey'); + let keyData = await cipherOrWrap( kWebCryptoCipherDecrypt, unwrapAlgo, @@ -979,7 +990,7 @@ async function unwrapKey( } return ReflectApply( - importKey, + importKeySync, this, [format, keyData, unwrappedKeyAlgo, extractable, keyUsages], ); @@ -1287,8 +1298,8 @@ async function encapsulateKey(encapsulationAlgorithm, encapsulationKey, sharedKe throw lazyDOMException('Unrecognized algorithm name', 'NotSupportedError'); } - const sharedKey = await ReflectApply( - importKey, + const sharedKey = ReflectApply( + importKeySync, this, ['raw-secret', encapsulateBits.sharedKey, normalizedSharedKeyAlgorithm, extractable, usages], ); @@ -1408,7 +1419,7 @@ async function decapsulateKey( } return ReflectApply( - importKey, + importKeySync, this, ['raw-secret', decapsulatedBits, normalizedSharedKeyAlgorithm, extractable, usages], );