From eaa0542eff274179a0befa80028b0e06628263bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Nie=C3=9Fen?= Date: Thu, 13 Apr 2017 15:43:04 +0200 Subject: [PATCH] crypto: remove unused C++ parameter in sign/verify 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: https://github.com/nodejs/node/pull/12397 Reviewed-By: Sam Roberts Reviewed-By: Anna Henningsen Reviewed-By: Colin Ihrig Reviewed-By: James M Snell --- lib/crypto.js | 4 ++-- src/node_crypto.cc | 55 +++++++++++++--------------------------------- 2 files changed, 17 insertions(+), 42 deletions(-) diff --git a/lib/crypto.js b/lib/crypto.js index e4a320a76e..880c12d1b6 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -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; @@ -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); }; diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 14079d9caf..fe1972299e 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -4194,24 +4194,20 @@ void Sign::SignFinal(const FunctionCallbackInfo& 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 maybe_padding = args[3]->Int32Value(env->context()); + CHECK(args[2]->IsInt32()); + Maybe maybe_padding = args[2]->Int32Value(env->context()); CHECK(maybe_padding.IsJust()); int padding = maybe_padding.ToChecked(); - CHECK(args[4]->IsInt32()); - Maybe maybe_salt_len = args[4]->Int32Value(env->context()); + CHECK(args[3]->IsInt32()); + Maybe maybe_salt_len = args[3]->Int32Value(env->context()); CHECK(maybe_salt_len.IsJust()); int salt_len = maybe_salt_len.ToChecked(); @@ -4224,7 +4220,7 @@ void Sign::SignFinal(const FunctionCallbackInfo& 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, @@ -4236,10 +4232,9 @@ void Sign::SignFinal(const FunctionCallbackInfo& args) { return sign->CheckThrow(err); } - Local rc = StringBytes::Encode(env->isolate(), - reinterpret_cast(md_value), - md_len, - encoding); + Local rc = Buffer::Copy(env->isolate(), + reinterpret_cast(md_value), + md_len).ToLocalChecked(); delete[] md_value; args.GetReturnValue().Set(rc); } @@ -4442,42 +4437,22 @@ void Verify::VerifyFinal(const FunctionCallbackInfo& 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 maybe_padding = args[3]->Int32Value(env->context()); + CHECK(args[2]->IsInt32()); + Maybe maybe_padding = args[2]->Int32Value(env->context()); CHECK(maybe_padding.IsJust()); int padding = maybe_padding.ToChecked(); - CHECK(args[4]->IsInt32()); - Maybe maybe_salt_len = args[4]->Int32Value(env->context()); + CHECK(args[3]->IsInt32()); + Maybe 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);