Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

crypto: add checkIsArrayBufferView #20251

Closed
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
17 changes: 3 additions & 14 deletions lib/internal/crypto/pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ const {
ERR_OUT_OF_RANGE
} = require('internal/errors').codes;
const {
checkIsArrayBufferView,
getDefaultEncoding,
toBuf
} = require('internal/crypto/util');
const { isArrayBufferView } = require('internal/util/types');
const {
PBKDF2
} = process.binding('crypto');
Expand Down Expand Up @@ -39,19 +39,8 @@ function _pbkdf2(password, salt, iterations, keylen, digest, callback) {
if (digest !== null && typeof digest !== 'string')
throw new ERR_INVALID_ARG_TYPE('digest', ['string', 'null'], digest);

password = toBuf(password);
salt = toBuf(salt);

if (!isArrayBufferView(password)) {
throw new ERR_INVALID_ARG_TYPE('password',
['string', 'Buffer', 'TypedArray'],
password);
}

if (!isArrayBufferView(salt)) {
throw new ERR_INVALID_ARG_TYPE('salt',
['string', 'Buffer', 'TypedArray'], salt);
}
password = checkIsArrayBufferView('password', toBuf(password));
salt = checkIsArrayBufferView('salt', toBuf(salt));

if (typeof iterations !== 'number')
throw new ERR_INVALID_ARG_TYPE('iterations', 'number', iterations);
Expand Down
39 changes: 6 additions & 33 deletions lib/internal/crypto/sig.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ const {
RSA_PKCS1_PADDING
} = process.binding('constants').crypto;
const {
checkIsArrayBufferView,
getDefaultEncoding,
toBuf
} = require('internal/crypto/util');
const { isArrayBufferView } = require('internal/util/types');
const { Writable } = require('stream');
const { inherits } = require('util');

Expand All @@ -41,14 +41,7 @@ Sign.prototype._write = function _write(chunk, encoding, callback) {

Sign.prototype.update = function update(data, encoding) {
encoding = encoding || getDefaultEncoding();
data = toBuf(data, encoding);
if (!isArrayBufferView(data)) {
throw new ERR_INVALID_ARG_TYPE(
'data',
['string', 'Buffer', 'TypedArray', 'DataView'],
data
);
}
data = checkIsArrayBufferView('data', toBuf(data, encoding));
this._handle.update(data);
return this;
};
Expand Down Expand Up @@ -79,14 +72,7 @@ Sign.prototype.sign = function sign(options, encoding) {
}
}

key = toBuf(key);
if (!isArrayBufferView(key)) {
throw new ERR_INVALID_ARG_TYPE(
'key',
['string', 'Buffer', 'TypedArray', 'DataView'],
key
);
}
key = checkIsArrayBufferView('key', toBuf(key));

var ret = this._handle.sign(key, passphrase, rsaPadding, pssSaltLength);

Expand Down Expand Up @@ -137,23 +123,10 @@ Verify.prototype.verify = function verify(options, signature, sigEncoding) {
}
}

key = toBuf(key);
if (!isArrayBufferView(key)) {
throw new ERR_INVALID_ARG_TYPE(
'key',
['string', 'Buffer', 'TypedArray', 'DataView'],
key
);
}
key = checkIsArrayBufferView('key', toBuf(key));

signature = toBuf(signature, sigEncoding);
if (!isArrayBufferView(signature)) {
throw new ERR_INVALID_ARG_TYPE(
'signature',
['string', 'Buffer', 'TypedArray', 'DataView'],
signature
);
}
signature = checkIsArrayBufferView('signature',
toBuf(signature, sigEncoding));

return this._handle.verify(key, signature, rsaPadding, pssSaltLength);
};
Expand Down
12 changes: 12 additions & 0 deletions lib/internal/crypto/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,19 @@ function timingSafeEqual(buf1, buf2) {
return _timingSafeEqual(buf1, buf2);
}

function checkIsArrayBufferView(name, buffer) {
if (!isArrayBufferView(buffer)) {
throw new ERR_INVALID_ARG_TYPE(
name,
['string', 'Buffer', 'TypedArray', 'DataView'],
buffer
);
}
return buffer;
}

module.exports = {
checkIsArrayBufferView,
getCiphers,
getCurves,
getDefaultEncoding,
Expand Down
3 changes: 2 additions & 1 deletion test/parallel/test-crypto-pbkdf2.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ assert.throws(
});

[1, {}, [], true, undefined, null].forEach((input) => {
const msgPart2 = `Buffer, or TypedArray. Received type ${typeof input}`;
const msgPart2 = 'Buffer, TypedArray, or DataView.' +
` Received type ${typeof input}`;
assert.throws(
() => crypto.pbkdf2(input, 'salt', 8, 8, 'sha256', common.mustNotCall()),
{
Expand Down