summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKai Groner <kai@gronr.com>2013-04-18 19:01:14 -0400
committerFedor Indutny <fedor.indutny@gmail.com>2013-12-04 19:52:15 +0400
commit98be8df571f92ad0b846209c21cc00139bc14805 (patch)
tree0b36a1d114063b747f8ae7955b4a991bf4f77252
parentb371d4ae8fdbf1b9046b9076cae1ee5fdc196724 (diff)
downloadnode-98be8df571f92ad0b846209c21cc00139bc14805.tar.gz
crypto: Make Decipher._flush() emit errors.
When Decipher processes a stream using an incorrect key, the DecipherFinal() method throws an unhandled exception at the end of the stream.
-rw-r--r--lib/crypto.js7
-rw-r--r--test/simple/test-crypto-stream.js15
2 files changed, 21 insertions, 1 deletions
diff --git a/lib/crypto.js b/lib/crypto.js
index 0cc70ff15..22141ff8a 100644
--- a/lib/crypto.js
+++ b/lib/crypto.js
@@ -263,7 +263,12 @@ Cipher.prototype._transform = function(chunk, encoding, callback) {
};
Cipher.prototype._flush = function(callback) {
- this.push(this._binding.final());
+ try {
+ this.push(this._binding.final());
+ } catch (e) {
+ callback(e);
+ return;
+ }
callback();
};
diff --git a/test/simple/test-crypto-stream.js b/test/simple/test-crypto-stream.js
index b51516f7d..72c9776d0 100644
--- a/test/simple/test-crypto-stream.js
+++ b/test/simple/test-crypto-stream.js
@@ -60,3 +60,18 @@ crypto.createHash('md5').unpipe({});
crypto.createHash('md5').setEncoding('utf8');
crypto.createHash('md5').pause();
crypto.createHash('md5').resume();
+
+// Decipher._flush() should emit an error event, not an exception.
+var key = new Buffer('48fb56eb10ffeb13fc0ef551bbca3b1b', 'hex'),
+ badkey = new Buffer('12341234123412341234123412341234', 'hex'),
+ iv = new Buffer('6d358219d1f488f5f4eb12820a66d146', 'hex'),
+ cipher = crypto.createCipheriv('aes-128-cbc', key, iv),
+ decipher = crypto.createDecipheriv('aes-128-cbc', badkey, iv);
+
+cipher.pipe(decipher)
+ .on('error', common.mustCall(function end(err) {
+ // TypeError: error:06065064:digital envelope routines:EVP_DecryptFinal_ex:bad decrypt
+ assert(/:06065064:/.test(err));
+ }));
+
+cipher.end('Papaya!'); // Should not cause an unhandled exception.