Skip to content

Commit

Permalink
crypto: remove unused C++ parameter in sign/verify
Browse files Browse the repository at this point in the history
Removes code in node_crypto.cc in Sign::SignFinal and
Verify::VerifyFinal which allowed to convert between buffers and
strings based on given encodings. The code is unused as crypto.js
only passes in and expects buffers and does the conversion itself.
The encoding parameter was removed from both methods.

PR-URL: #12397
Reviewed-By: Sam Roberts <[email protected]>
Reviewed-By: Anna Henningsen <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
tniessen authored and jasnell committed Apr 18, 2017
1 parent 9e26347 commit eaa0542
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 42 deletions.
4 changes: 2 additions & 2 deletions lib/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ Sign.prototype.sign = function sign(options, encoding) {
}
}

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

encoding = encoding || exports.DEFAULT_ENCODING;
Expand Down Expand Up @@ -376,7 +376,7 @@ Verify.prototype.verify = function verify(options, signature, sigEncoding) {
}
}

return this._handle.verify(toBuf(key), toBuf(signature, sigEncoding), null,
return this._handle.verify(toBuf(key), toBuf(signature, sigEncoding),
rsaPadding, pssSaltLength);
};

Expand Down
55 changes: 15 additions & 40 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4194,24 +4194,20 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) {
unsigned int md_len;

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

node::Utf8Value passphrase(env->isolate(), args[2]);
node::Utf8Value passphrase(env->isolate(), args[1]);

THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Data");
size_t buf_len = Buffer::Length(args[0]);
char* buf = Buffer::Data(args[0]);

CHECK(args[3]->IsInt32());
Maybe<int32_t> maybe_padding = args[3]->Int32Value(env->context());
CHECK(args[2]->IsInt32());
Maybe<int32_t> maybe_padding = args[2]->Int32Value(env->context());
CHECK(maybe_padding.IsJust());
int padding = maybe_padding.ToChecked();

CHECK(args[4]->IsInt32());
Maybe<int32_t> maybe_salt_len = args[4]->Int32Value(env->context());
CHECK(args[3]->IsInt32());
Maybe<int32_t> maybe_salt_len = args[3]->Int32Value(env->context());
CHECK(maybe_salt_len.IsJust());
int salt_len = maybe_salt_len.ToChecked();

Expand All @@ -4224,7 +4220,7 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) {
Error err = sign->SignFinal(
buf,
buf_len,
len >= 3 && !args[2]->IsNull() ? *passphrase : nullptr,
len >= 2 && !args[1]->IsNull() ? *passphrase : nullptr,
&md_value,
&md_len,
padding,
Expand All @@ -4236,10 +4232,9 @@ void Sign::SignFinal(const FunctionCallbackInfo<Value>& args) {
return sign->CheckThrow(err);
}

Local<Value> rc = StringBytes::Encode(env->isolate(),
reinterpret_cast<const char*>(md_value),
md_len,
encoding);
Local<Object> rc = Buffer::Copy(env->isolate(),
reinterpret_cast<const char*>(md_value),
md_len).ToLocalChecked();
delete[] md_value;
args.GetReturnValue().Set(rc);
}
Expand Down Expand Up @@ -4442,42 +4437,22 @@ void Verify::VerifyFinal(const FunctionCallbackInfo<Value>& args) {

THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[1], "Hash");

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

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

// only copy if we need to, because it's a string.
char* hbuf;
if (args[1]->IsString()) {
hbuf = new char[hlen];
ssize_t hwritten = StringBytes::Write(env->isolate(),
hbuf,
hlen,
args[1],
encoding);
CHECK_EQ(hwritten, hlen);
} else {
hbuf = Buffer::Data(args[1]);
}
char* hbuf = Buffer::Data(args[1]);
ssize_t hlen = Buffer::Length(args[1]);

CHECK(args[3]->IsInt32());
Maybe<int32_t> maybe_padding = args[3]->Int32Value(env->context());
CHECK(args[2]->IsInt32());
Maybe<int32_t> maybe_padding = args[2]->Int32Value(env->context());
CHECK(maybe_padding.IsJust());
int padding = maybe_padding.ToChecked();

CHECK(args[4]->IsInt32());
Maybe<int32_t> maybe_salt_len = args[4]->Int32Value(env->context());
CHECK(args[3]->IsInt32());
Maybe<int32_t> maybe_salt_len = args[3]->Int32Value(env->context());
CHECK(maybe_salt_len.IsJust());
int salt_len = maybe_salt_len.ToChecked();

bool verify_result;
Error err = verify->VerifyFinal(kbuf, klen, hbuf, hlen, padding, salt_len,
&verify_result);
if (args[1]->IsString())
delete[] hbuf;
if (err != kSignOk)
return verify->CheckThrow(err);
args.GetReturnValue().Set(verify_result);
Expand Down

0 comments on commit eaa0542

Please sign in to comment.