|
|
@ -4101,22 +4101,16 @@ class DiffieHellman : public ObjectWrap { |
|
|
|
|
|
|
|
int size = DH_compute_key(reinterpret_cast<unsigned char*>(data), |
|
|
|
key, diffieHellman->dh); |
|
|
|
BN_free(key); |
|
|
|
|
|
|
|
Local<Value> outString; |
|
|
|
|
|
|
|
// DH_size returns number of bytes in a prime number
|
|
|
|
// DH_compute_key returns number of bytes in a remainder of exponent, which
|
|
|
|
// may have less bytes than a prime number. Therefore add 0-padding to the
|
|
|
|
// allocated buffer.
|
|
|
|
if (size != dataSize) { |
|
|
|
assert(dataSize > size); |
|
|
|
memset(data + size, 0, dataSize - size); |
|
|
|
} |
|
|
|
|
|
|
|
if (size == -1) { |
|
|
|
int checkResult; |
|
|
|
if (!DH_check_pub_key(diffieHellman->dh, key, &checkResult)) { |
|
|
|
int checked; |
|
|
|
|
|
|
|
checked = DH_check_pub_key(diffieHellman->dh, key, &checkResult); |
|
|
|
BN_free(key); |
|
|
|
delete[] data; |
|
|
|
|
|
|
|
if (!checked) { |
|
|
|
return ThrowException(Exception::Error(String::New("Invalid key"))); |
|
|
|
} else if (checkResult) { |
|
|
|
if (checkResult & DH_CHECK_PUBKEY_TOO_SMALL) { |
|
|
@ -4131,14 +4125,28 @@ class DiffieHellman : public ObjectWrap { |
|
|
|
} else { |
|
|
|
return ThrowException(Exception::Error(String::New("Invalid key"))); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
BN_free(key); |
|
|
|
assert(size >= 0); |
|
|
|
|
|
|
|
// DH_size returns number of bytes in a prime number
|
|
|
|
// DH_compute_key returns number of bytes in a remainder of exponent, which
|
|
|
|
// may have less bytes than a prime number. Therefore add 0-padding to the
|
|
|
|
// allocated buffer.
|
|
|
|
if (size != dataSize) { |
|
|
|
assert(dataSize > size); |
|
|
|
memset(data + size, 0, dataSize - size); |
|
|
|
} |
|
|
|
|
|
|
|
Local<Value> outString; |
|
|
|
|
|
|
|
if (args.Length() > 2 && args[2]->IsString()) { |
|
|
|
outString = EncodeWithEncoding(args[2], data, dataSize); |
|
|
|
} else if (args.Length() > 1 && args[1]->IsString()) { |
|
|
|
outString = EncodeWithEncoding(args[1], data, dataSize); |
|
|
|
} else { |
|
|
|
if (args.Length() > 2 && args[2]->IsString()) { |
|
|
|
outString = EncodeWithEncoding(args[2], data, dataSize); |
|
|
|
} else if (args.Length() > 1 && args[1]->IsString()) { |
|
|
|
outString = EncodeWithEncoding(args[1], data, dataSize); |
|
|
|
} else { |
|
|
|
outString = Encode(data, dataSize, BINARY); |
|
|
|
} |
|
|
|
outString = Encode(data, dataSize, BINARY); |
|
|
|
} |
|
|
|
|
|
|
|
delete[] data; |
|
|
|