Permalink
Browse files

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
PR-URL: #12752
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Refael Ackermann <refack@gmail.com>
Reviewed-By: Gibson Fahnestock <gibfahn@gmail.com>
  • Loading branch information...
addaleax committed Apr 29, 2017
1 parent 9990be2 commit 6c2daf0ce9cffbadf51359ddfb804f5a6107737c
Showing with 18 additions and 4 deletions.
  1. +8 −0 src/node_crypto.cc
  2. +6 −4 test/parallel/test-crypto-hash.js
  3. +4 −0 test/parallel/test-crypto-hmac.js
View
@@ -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;
@@ -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;
@@ -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$/);
@@ -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$/);

0 comments on commit 6c2daf0

Please sign in to comment.