Skip to content

Commit

Permalink
crypto: handle exceptions in hmac/hash.digest
Browse files Browse the repository at this point in the history
Forced conversion of the encoding parameter to a string within
crypto.js, fixing segmentation faults in node_crypto.cc.

Fixes: nodejs#9819
  • Loading branch information
tniessen committed Apr 2, 2017
1 parent 0ea4570 commit 768c986
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
3 changes: 2 additions & 1 deletion lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ Hash.prototype.update = function update(data, encoding) {

Hash.prototype.digest = function digest(outputEncoding) {
outputEncoding = outputEncoding || exports.DEFAULT_ENCODING;
return this._handle.digest(outputEncoding);
// Explicit conversion for backward compatibility
return this._handle.digest(String(outputEncoding));
};


Expand Down
2 changes: 2 additions & 0 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1481,6 +1481,8 @@ enum encoding ParseEncoding(const char* encoding,
enum encoding ParseEncoding(Isolate* isolate,
Local<Value> encoding_v,
enum encoding default_encoding) {
CHECK(!encoding_v.IsEmpty());

if (!encoding_v->IsString())
return default_encoding;

Expand Down
18 changes: 5 additions & 13 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3797,9 +3797,7 @@ void Hmac::HmacDigest(const FunctionCallbackInfo<Value>& args) {

enum encoding encoding = BUFFER;
if (args.Length() >= 1) {
encoding = ParseEncoding(env->isolate(),
args[0]->ToString(env->isolate()),
BUFFER);
encoding = ParseEncoding(env->isolate(), args[0], BUFFER);
}

unsigned char* md_value = nullptr;
Expand Down Expand Up @@ -3921,9 +3919,7 @@ void Hash::HashDigest(const FunctionCallbackInfo<Value>& args) {

enum encoding encoding = BUFFER;
if (args.Length() >= 1) {
encoding = ParseEncoding(env->isolate(),
args[0]->ToString(env->isolate()),
BUFFER);
encoding = ParseEncoding(env->isolate(), args[0], BUFFER);
}

unsigned char md_value[EVP_MAX_MD_SIZE];
Expand Down Expand Up @@ -4201,10 +4197,8 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) {

unsigned int len = args.Length();
enum encoding encoding = BUFFER;
if (len >= 2 && args[1]->IsString()) {
encoding = ParseEncoding(env->isolate(),
args[1]->ToString(env->isolate()),
BUFFER);
if (len >= 2) {
encoding = ParseEncoding(env->isolate(), args[1], BUFFER);
}

node::Utf8Value passphrase(env->isolate(), args[2]);
Expand Down Expand Up @@ -4452,9 +4446,7 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {

enum encoding encoding = UTF8;
if (args.Length() >= 3) {
encoding = ParseEncoding(env->isolate(),
args[2]->ToString(env->isolate()),
UTF8);
encoding = ParseEncoding(env->isolate(), args[2], UTF8);
}

ssize_t hlen = StringBytes::Size(env->isolate(), args[1], encoding);
Expand Down

0 comments on commit 768c986

Please sign in to comment.