E5E6 crypto: implement rfc7517 recommendation by hotpineapple · Pull Request #60221 · nodejs/node · GitHub
[go: up one dir, main page]

Skip to content
Open
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
22 changes: 18 additions & 4 deletions lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const {
ObjectKeys,
ObjectPrototypeHasOwnProperty,
Promise,
SafeSet,
StringPrototypeToUpperCase,
Symbol,
TypedArrayPrototypeGetBuffer,
Expand Down Expand Up @@ -757,9 +758,26 @@ const kKeyOps = {
deriveBits: 8,
};

const allowedGroups = [
new SafeSet(['sign', 'verify']),
new SafeSet(['encrypt', 'decrypt']),
new SafeSet(['wrapKey', 'unwrapKey']),
];

function validateKeyOps(keyOps, usagesSet) {
if (keyOps === undefined) return;
validateArray(keyOps, 'keyData.key_ops');
const keyOpsSet = new SafeSet(keyOps);
const isValidCombo = allowedGroups.some((group) => {
return [...keyOpsSet].every((op) => group.has(op));
});
if (!isValidCombo && keyOpsSet.size > 1) {
process.emitWarning(
'Using unrelated key_ops combinations (RFC7517 section 4.3) is deprecated and will throw in a future version.',
'DeprecationWarning',
);
}

let flags = 0;
for (let n = 0; n < keyOps.length; n++) {
const op = keyOps[n];
Expand All @@ -771,10 +789,6 @@ function validateKeyOps(keyOps, usagesSet) {
if (flags & (1 << op_flag))
throw lazyDOMException('Duplicate key operation', 'DataError');
flags |= (1 << op_flag);

// TODO(@jasnell): RFC7517 section 4.3 strong recommends validating
// key usage combinations. Specifically, it says that unrelated key
// ops SHOULD NOT be used together. We're not yet validating that here.
}

if (usagesSet !== undefined) {
Expand Down
Loading
0