Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
restore support for null passphase
  • Loading branch information
danbev committed May 6, 2017
commit 3a59be895e18fa524b67bb985a723f16b774bbc3
29 changes: 15 additions & 14 deletions src/node_crypto.cc
Original file line number Diff line number Diff line change
Expand Up @@ -229,12 +229,15 @@ static void crypto_lock_cb(int mode, int n, const char* file, int line) {


static int PasswordCallback(char *buf, int size, int rwflag, void *u) {
CHECK_NE(u, nullptr);
size_t buflen = static_cast<size_t>(size);
size_t len = strlen(static_cast<const char*>(u));
len = len > buflen ? buflen : len;
memcpy(buf, u, len);
return len;
if (u) {
size_t buflen = static_cast<size_t>(size);
size_t len = strlen(static_cast<const char*>(u));
len = len > buflen ? buflen : len;
memcpy(buf, u, len);
return len;
}

return 0;
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add one more newline here, please.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll fix that.


Expand Down Expand Up @@ -473,10 +476,9 @@ void SecureContext::SetKey(const FunctionCallbackInfo<Value>& args) {
return env->ThrowError("Only private key and pass phrase are expected");
}

bool has_password = len == 2;
if (has_password) {
if (len == 2) {
if (args[1]->IsUndefined() || args[1]->IsNull())
has_password = false;
len = 1;
else
THROW_AND_RETURN_IF_NOT_STRING(args[1], "Pass phrase");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if len == 1 from the start? It looks like has_password will be true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, sorry. Let me fix that. Thanks

Expand All @@ -485,13 +487,12 @@ void SecureContext::SetKey(const FunctionCallbackInfo<Value>& args) {
if (!bio)
return;

auto callback = has_password ? PasswordCallback : NoPasswordCallback;
auto passphrase = has_password ?
*node::Utf8Value{env->isolate(), args[1]} : nullptr;
node::Utf8Value passphrase(env->isolate(), args[1]);

EVP_PKEY* key = PEM_read_bio_PrivateKey(bio,
nullptr,
callback,
passphrase);
PasswordCallback,
len == 1 ? nullptr : *passphrase);

if (!key) {
BIO_free_all(bio);
Expand Down