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
crypto: throw proper errors if out enc is UTF-16
Throw `Error`s instead of hard crashing when the `.digest()` output
encoding is UTF-16.

Fixes: #9817
  • Loading branch information
addaleax committed Apr 29, 2017
commit 95ed40b5f91c98e6be4268b6fdf2e0020e9c8e78
8 changes: 8 additions & 0 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3798,6 +3798,10 @@ void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {
encoding = ParseEncoding(env->isolate(), args[0], BUFFER);
}

if (encoding == UCS2) {
return env->ThrowError("hmac.digest() does not support UTF-16");
}

unsigned char* md_value = nullptr;
unsigned int md_len = 0;

Expand Down Expand Up @@ -3921,6 +3925,10 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {
encoding = ParseEncoding(env->isolate(), args[0], BUFFER);
}

if (encoding == UCS2) {
return env->ThrowError("hash.digest() does not support UTF-16");
}

unsigned char md_value[EVP_MAX_MD_SIZE];
unsigned int md_len;

Expand Down
10 changes: 6 additions & 4 deletions test/parallel/test-crypto-hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,10 +108,12 @@ const h3 = crypto.createHash('sha256');
h3.digest();
assert.throws(function() {
h3.digest();
},
/Digest already called/);
}, /Digest already called/);

assert.throws(function() {
h3.update('foo');
},
/Digest already called/);
}, /Digest already called/);

assert.throws(function() {
crypto.createHash('sha256').digest('ucs2');
}, /^Error: hash\.digest\(\) does not support UTF-16$/);
4 changes: 4 additions & 0 deletions test/parallel/test-crypto-hmac.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,7 @@ for (let i = 0, l = rfc2202_sha1.length; i < l; i++) {
`Test HMAC-SHA1 : Test case ${i + 1} rfc 2202`
);
}

assert.throws(function() {
crypto.createHmac('sha256', 'w00t').digest('ucs2');
}, /^Error: hmac\.digest\(\) does not support UTF-16$/);