crypto: evict getCiphers/getHashes cache on setFips/setEngine by srikanth-karthi · Pull Request #62985 · nodejs/node · GitHub
Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/crypto.js
24 changes: 22 additions & 2 deletions lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const {
ArrayFrom,
ArrayPrototypeIncludes,
ArrayPrototypePush,
ArrayPrototypeSlice,
BigInt,
DataViewPrototypeGetBuffer,
DataViewPrototypeGetByteLength,
Expand Down Expand Up @@ -125,8 +126,25 @@ function getCachedHashId(algorithm) {
return result === undefined ? -1 : result;
}

const getCiphers = cachedResult(() => filterDuplicateStrings(_getCiphers()));
const getHashes = cachedResult(() => filterDuplicateStrings(_getHashes()));
let _ciphersCache;
function getCiphers() {
if (_ciphersCache === undefined)
_ciphersCache = filterDuplicateStrings(_getCiphers());
return ArrayPrototypeSlice(_ciphersCache);
}

let _hashesCache;
function getHashes() {
if (_hashesCache === undefined)
_hashesCache = filterDuplicateStrings(_getHashes());
return ArrayPrototypeSlice(_hashesCache);
}

function evictCipherHashCache() {
_ciphersCache = undefined;
_hashesCache = undefined;
}

const getCurves = cachedResult(() => filterDuplicateStrings(_getCurves()));

function setEngine(id, flags) {
Expand All @@ -143,6 +161,7 @@ function setEngine(id, flags) {
throw new ERR_CRYPTO_CUSTOM_ENGINE_NOT_SUPPORTED();
if (!_setEngine(id, flags))
throw new ERR_CRYPTO_ENGINE_UNKNOWN(id);
evictCipherHashCache();
}

const getArrayBufferOrView = hideStackFrames((buffer, name, encoding) => {
Expand Down Expand Up @@ -855,6 +874,7 @@ module.exports = {
getCurves,
getDataViewOrTypedArrayBuffer,
getHashes,
evictCipherHashCache,
kHandle,
kKeyObject,
setEngine,
Expand Down
74 changes: 74 additions & 0 deletions test/parallel/test-crypto-ciphers-hashes-fips-cache.js
Loading