镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Commit f4665f1

Browse files
panvaaduh95
authored andcommitted
crypto: validate the limit of PBKDF2 iterations
Reject iteration counts outside of OpenSSL supported range Signed-off-by: Filip Skokan <panva.ip@gmail.com> PR-URL: #65704 Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com>
1 parent 624abbe commit f4665f1

3 files changed

Lines changed: 32 additions & 0 deletions

File tree

‎lib/internal/crypto/webidl.js‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const {
1717
kEmptyObject,
1818
} = require('internal/util');
1919
const {
20+
isInt32,
2021
isUint32,
2122
} = require('internal/validators');
2223
const { CryptoKey } = require('internal/crypto/webcrypto');
@@ -504,6 +505,11 @@ converters.Pbkdf2Params = createDictionaryConverter(
504505
validator: (V, dict) => {
505506
if (V === 0)
506507
throw lazyDOMException('iterations cannot be zero', 'OperationError');
508+
if (!isInt32(V)) {
509+
throw lazyDOMException(
510+
'iterations exceeds the implementation limit',
511+
'NotSupportedError');
512+
}
507513
},
508514
required: true,
509515
},

‎test/fixtures/webcrypto/supports-level-2.mjs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ export const vectors = {
124124
[true,
125125
{ name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 1 },
126126
{ name: 'HMAC', hash: 'SHA-256' }],
127+
[false,
128+
{ name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 2 ** 31 },
129+
{ name: 'AES-CBC', length: 128 }],
127130
[false,
128131
{ name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 1 },
129132
'HKDF'],
@@ -183,6 +186,7 @@ export const vectors = {
183186
[true, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 1 }, 8],
184187
[true, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 1 }, 0],
185188
[false, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 0 }, 8],
189+
[false, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 2 ** 31 }, 8],
186190
[false, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 1 }, null],
187191
[false, { name: 'PBKDF2', hash: 'SHA-256', salt: Buffer.alloc(0), iterations: 1 }, 7],
188192
[false, { name: 'PBKDF2', hash: 'Invalid', salt: Buffer.alloc(0), iterations: 1 }, 8],

‎test/parallel/test-webcrypto-derivebits.js‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,28 @@ const { subtle } = globalThis.crypto;
101101
tests.then(common.mustCall());
102102
}
103103

104+
// Test PBKDF2 rejects iteration counts beyond the native signed int range
105+
{
106+
async function test() {
107+
const key = await subtle.importKey(
108+
'raw',
109+
new Uint8Array([1]),
110+
'PBKDF2',
111+
false,
112+
['deriveBits']);
113+
await assert.rejects(
114+
subtle.deriveBits({
115+
name: 'PBKDF2',
116+
hash: 'SHA-256',
117+
salt: new Uint8Array([2]),
118+
iterations: 2 ** 31,
119+
}, key, 8),
120+
{ name: 'NotSupportedError' });
121+
}
122+
123+
test().then(common.mustCall());
124+
}
125+
104126
// Test X25519 and X448 bit derivation
105127
{
106128
async function test(name) {

0 commit comments

Comments
 (0)