diff options
author | James M Snell <jasnell@gmail.com> | 2017-10-02 20:56:49 -0700 |
---|---|---|
committer | James M Snell <jasnell@gmail.com> | 2017-10-23 15:52:11 -0700 |
commit | 7124b466d9c10e12b3bd9a59910032e033f35493 (patch) | |
tree | e630cbe2299127b116f6f21d41da0b0a7aacd9c3 /src/node_crypto.cc | |
parent | 4eb9365d6641ab0aaface2528404d00ec20f98c5 (diff) | |
download | android-node-v8-7124b466d9c10e12b3bd9a59910032e033f35493.tar.gz android-node-v8-7124b466d9c10e12b3bd9a59910032e033f35493.tar.bz2 android-node-v8-7124b466d9c10e12b3bd9a59910032e033f35493.zip |
crypto: refactor argument validation for pbkdf2
Move input argument validation to js, using internal/errors.
Also update docs
* `password` and `salt` may be Buffers or any TypedArrays
* `crypto.DEFAULT_ENCODING` changes the returned derivedKey type
PR-URL: https://github.com/nodejs/node/pull/15746
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com>
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Diffstat (limited to 'src/node_crypto.cc')
-rw-r--r-- | src/node_crypto.cc | 48 |
1 files changed, 4 insertions, 44 deletions
diff --git a/src/node_crypto.cc b/src/node_crypto.cc index fa4815a989..3e8dd4205f 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -5330,7 +5330,6 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) { Environment* env = Environment::GetCurrent(args); const EVP_MD* digest = nullptr; - const char* type_error = nullptr; char* pass = nullptr; char* salt = nullptr; int passlen = -1; @@ -5341,54 +5340,19 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) { PBKDF2Request* req = nullptr; Local<Object> obj; - if (args.Length() != 5 && args.Length() != 6) { - type_error = "Bad parameter"; - goto err; - } - - THROW_AND_RETURN_IF_NOT_BUFFER(args[0], "Pass phrase"); passlen = Buffer::Length(args[0]); - if (passlen < 0) { - type_error = "Bad password"; - goto err; - } - - THROW_AND_RETURN_IF_NOT_BUFFER(args[1], "Salt"); pass = node::Malloc(passlen); memcpy(pass, Buffer::Data(args[0]), passlen); saltlen = Buffer::Length(args[1]); - if (saltlen < 0) { - type_error = "Bad salt"; - goto err; - } salt = node::Malloc(saltlen); memcpy(salt, Buffer::Data(args[1]), saltlen); - if (!args[2]->IsNumber()) { - type_error = "Iterations not a number"; - goto err; - } - iter = args[2]->Int32Value(); - if (iter < 0) { - type_error = "Bad iterations"; - goto err; - } - - if (!args[3]->IsNumber()) { - type_error = "Key length not a number"; - goto err; - } raw_keylen = args[3]->NumberValue(); - if (raw_keylen < 0.0 || isnan(raw_keylen) || isinf(raw_keylen) || - raw_keylen > INT_MAX) { - type_error = "Bad key length"; - goto err; - } keylen = static_cast<int>(raw_keylen); @@ -5396,8 +5360,10 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) { node::Utf8Value digest_name(env->isolate(), args[4]); digest = EVP_get_digestbyname(*digest_name); if (digest == nullptr) { - type_error = "Bad digest name"; - goto err; + free(salt); + free(pass); + args.GetReturnValue().Set(-1); + return; } } @@ -5443,12 +5409,6 @@ void PBKDF2(const FunctionCallbackInfo<Value>& args) { else args.GetReturnValue().Set(argv[1]); } - return; - - err: - free(salt); - free(pass); - return env->ThrowTypeError(type_error); } |