summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan Kelly <ryan@rfk.id.au>2017-05-29 14:20:04 +1000
committerMyles Borins <mylesborins@google.com>2017-07-11 17:44:23 +0100
commit61f046404dd2bdc954a2a27dec8b92e867eb9927 (patch)
treee7d9841b34cfd501c10c705ef91a72539118d085
parent0bcd62baeec0571b9beeb7956009fb077f01bb7f (diff)
downloadnode-new-61f046404dd2bdc954a2a27dec8b92e867eb9927.tar.gz
crypto: clear err stack after ECDH::BufferToPoint
Functions that call `ECDH::BufferToPoint` were not clearing the error stack on failure, so an invalid key could leave leftover error state and cause subsequent (unrelated) signing operations to fail. PR-URL: https://github.com/nodejs/node/pull/13275 Backport-PR-URL: https://github.com/nodejs/node/pull/13399 Reviewed-By: Fedor Indutny <fedor.indutny@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Sam Roberts <vieuxtech@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
-rw-r--r--src/node_crypto.cc4
-rw-r--r--test/parallel/test-crypto-dh.js20
2 files changed, 24 insertions, 0 deletions
diff --git a/src/node_crypto.cc b/src/node_crypto.cc
index d447e25b63..13af291dfc 100644
--- a/src/node_crypto.cc
+++ b/src/node_crypto.cc
@@ -4916,6 +4916,8 @@ void ECDH::ComputeSecret(const FunctionCallbackInfo<Value>& args) {
ECDH* ecdh;
ASSIGN_OR_RETURN_UNWRAP(&ecdh, args.Holder());
+ MarkPopErrorOnReturn mark_pop_error_on_return;
+
EC_POINT* pub = ecdh->BufferToPoint(Buffer::Data(args[0]),
Buffer::Length(args[0]));
if (pub == nullptr)
@@ -5038,6 +5040,8 @@ void ECDH::SetPublicKey(const FunctionCallbackInfo<Value>& args) {
THROW_AND_RETURN_IF_NOT_BUFFER(args[0]);
+ MarkPopErrorOnReturn mark_pop_error_on_return;
+
EC_POINT* pub = ecdh->BufferToPoint(Buffer::Data(args[0].As<Object>()),
Buffer::Length(args[0].As<Object>()));
if (pub == nullptr)
diff --git a/test/parallel/test-crypto-dh.js b/test/parallel/test-crypto-dh.js
index e6b1de2475..29d33741ee 100644
--- a/test/parallel/test-crypto-dh.js
+++ b/test/parallel/test-crypto-dh.js
@@ -188,3 +188,23 @@ ecdh4.setPublicKey(ecdh1.getPublicKey());
assert.throws(function() {
ecdh4.setPublicKey(ecdh3.getPublicKey());
});
+
+// Use of invalid keys was not cleaning up ERR stack, and was causing
+// unexpected failure in subsequent signing operations.
+var ecdh5 = crypto.createECDH('prime256v1');
+var invalidKey = Buffer.alloc(65);
+invalidKey.fill('\0');
+ecdh5.generateKeys();
+assert.throws(() => {
+ ecdh5.computeSecret(invalidKey);
+}, /^Error: Failed to translate Buffer to a EC_POINT$/);
+// Check that signing operations are not impacted by the above error.
+const ecPrivateKey =
+ '-----BEGIN EC PRIVATE KEY-----\n' +
+ 'MHcCAQEEIF+jnWY1D5kbVYDNvxxo/Y+ku2uJPDwS0r/VuPZQrjjVoAoGCCqGSM49\n' +
+ 'AwEHoUQDQgAEurOxfSxmqIRYzJVagdZfMMSjRNNhB8i3mXyIMq704m2m52FdfKZ2\n' +
+ 'pQhByd5eyj3lgZ7m7jbchtdgyOF8Io/1ng==\n' +
+ '-----END EC PRIVATE KEY-----';
+assert.doesNotThrow(() => {
+ crypto.createSign('SHA256').sign(ecPrivateKey);
+});