From b31539a2b55a3f65f28bd2c01963ec2a6a43019b Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Sat, 30 Jul 2011 23:50:04 +0200 Subject: [PATCH] crypto: throw descriptive error when crypto object is not initialized --- src/node_crypto.cc | 75 ++++++++++++++++++++++++-------------- test/simple/test-crypto.js | 28 ++++++++++++++ 2 files changed, 75 insertions(+), 28 deletions(-) diff --git a/src/node_crypto.cc b/src/node_crypto.cc index f07bf785b20..65466b9fb66 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -63,6 +63,13 @@ static Persistent ext_key_usage_symbol; static Persistent secure_context_constructor; + +Handle NotInitializedException() { + HandleScope scope; + return ThrowException(Exception::Error(String::New("Not initialized"))); +} + + void SecureContext::Initialize(Handle target) { HandleScope scope; @@ -1801,10 +1808,13 @@ class Cipher : public ObjectWrap { } static Handle CipherUpdate(const Arguments& args) { - Cipher *cipher = ObjectWrap::Unwrap(args.This()); - HandleScope scope; + Cipher *cipher = ObjectWrap::Unwrap(args.This()); + if (!cipher->initialised_) { + return NotInitializedException(); + } + ASSERT_IS_STRING_OR_BUFFER(args[0]); enum encoding enc = ParseEncoding(args[1]); @@ -2185,6 +2195,9 @@ class Decipher : public ObjectWrap { HandleScope scope; Decipher *cipher = ObjectWrap::Unwrap(args.This()); + if (!cipher->initialised_) { + return NotInitializedException(); + } ASSERT_IS_STRING_OR_BUFFER(args[0]); @@ -2529,10 +2542,13 @@ class Hmac : public ObjectWrap { } static Handle HmacUpdate(const Arguments& args) { - Hmac *hmac = ObjectWrap::Unwrap(args.This()); - HandleScope scope; + Hmac *hmac = ObjectWrap::Unwrap(args.This()); + if (!hmac->initialised_) { + return NotInitializedException(); + } + ASSERT_IS_STRING_OR_BUFFER(args[0]); enum encoding enc = ParseEncoding(args[1]); ssize_t len = DecodeBytes(args[0], enc); @@ -2684,6 +2700,9 @@ class Hash : public ObjectWrap { HandleScope scope; Hash *hash = ObjectWrap::Unwrap(args.This()); + if (!hash->initialised_) { + return NotInitializedException(); + } ASSERT_IS_STRING_OR_BUFFER(args[0]); enum encoding enc = ParseEncoding(args[1]); @@ -2721,9 +2740,8 @@ class Hash : public ObjectWrap { HandleScope scope; Hash *hash = ObjectWrap::Unwrap(args.This()); - if (!hash->initialised_) { - return ThrowException(Exception::Error(String::New("Not initialized"))); + return NotInitializedException(); } unsigned char md_value[EVP_MAX_MD_SIZE]; @@ -2877,10 +2895,13 @@ class Sign : public ObjectWrap { } static Handle SignUpdate(const Arguments& args) { - Sign *sign = ObjectWrap::Unwrap(args.This()); - HandleScope scope; + Sign *sign = ObjectWrap::Unwrap(args.This()); + if (!sign->initialised_) { + return NotInitializedException(); + } + ASSERT_IS_STRING_OR_BUFFER(args[0]); enum encoding enc = ParseEncoding(args[1]); ssize_t len = DecodeBytes(args[0], enc); @@ -3332,14 +3353,13 @@ class DiffieHellman : public ObjectWrap { } static Handle GenerateKeys(const Arguments& args) { + HandleScope scope; + DiffieHellman* diffieHellman = ObjectWrap::Unwrap(args.This()); - HandleScope scope; - if (!diffieHellman->initialised_) { - return ThrowException(Exception::Error( - String::New("Not initialized"))); + return NotInitializedException(); } if (!DH_generate_key(diffieHellman->dh)) { @@ -3365,13 +3385,13 @@ class DiffieHellman : public ObjectWrap { } static Handle GetPrime(const Arguments& args) { + HandleScope scope; + DiffieHellman* diffieHellman = ObjectWrap::Unwrap(args.This()); - HandleScope scope; - if (!diffieHellman->initialised_) { - return ThrowException(Exception::Error(String::New("Not initialized"))); + return NotInitializedException(); } int dataSize = BN_num_bytes(diffieHellman->dh->p); @@ -3392,13 +3412,13 @@ class DiffieHellman : public ObjectWrap { } static Handle GetGenerator(const Arguments& args) { + HandleScope scope; + DiffieHellman* diffieHellman = ObjectWrap::Unwrap(args.This()); - HandleScope scope; - if (!diffieHellman->initialised_) { - return ThrowException(Exception::Error(String::New("Not initialized"))); + return NotInitializedException(); } int dataSize = BN_num_bytes(diffieHellman->dh->g); @@ -3419,13 +3439,13 @@ class DiffieHellman : public ObjectWrap { } static Handle GetPublicKey(const Arguments& args) { + HandleScope scope; + DiffieHellman* diffieHellman = ObjectWrap::Unwrap(args.This()); - HandleScope scope; - if (!diffieHellman->initialised_) { - return ThrowException(Exception::Error(String::New("Not initialized"))); + return NotInitializedException(); } if (diffieHellman->dh->pub_key == NULL) { @@ -3452,13 +3472,13 @@ class DiffieHellman : public ObjectWrap { } static Handle GetPrivateKey(const Arguments& args) { + HandleScope scope; + DiffieHellman* diffieHellman = ObjectWrap::Unwrap(args.This()); - HandleScope scope; - if (!diffieHellman->initialised_) { - return ThrowException(Exception::Error(String::New("Not initialized"))); + return NotInitializedException(); } if (diffieHellman->dh->priv_key == NULL) { @@ -3491,7 +3511,7 @@ class DiffieHellman : public ObjectWrap { ObjectWrap::Unwrap(args.This()); if (!diffieHellman->initialised_) { - return ThrowException(Exception::Error(String::New("Not initialized"))); + return NotInitializedException(); } BIGNUM* key = 0; @@ -3573,7 +3593,7 @@ class DiffieHellman : public ObjectWrap { ObjectWrap::Unwrap(args.This()); if (!diffieHellman->initialised_) { - return ThrowException(Exception::Error(String::New("Not initialized"))); + return NotInitializedException(); } if (args.Length() == 0) { @@ -3618,8 +3638,7 @@ class DiffieHellman : public ObjectWrap { ObjectWrap::Unwrap(args.This()); if (!diffieHellman->initialised_) { - return ThrowException(Exception::Error( - String::New("Not initialized"))); + return NotInitializedException(); } if (args.Length() == 0) { diff --git a/test/simple/test-crypto.js b/test/simple/test-crypto.js index b761b431ce6..4f39cc043f0 100644 --- a/test/simple/test-crypto.js +++ b/test/simple/test-crypto.js @@ -388,3 +388,31 @@ assert.equal(rsaSignature, '5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7 rsaVerify.update(rsaPubPem); assert.equal(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), 1); + +function expectNotInitialized(e) { + return e && e.message === 'Not initialized'; +} + +assert.throws(function() { + var o = crypto.createHash('sha1'); + o.digest(); + o.digest(); +}, expectNotInitialized); + +assert.throws(function() { + var o = crypto.createHash('sha1'); + o.digest(); + o.update(''); +}, expectNotInitialized); + +assert.throws(function() { + var o = crypto.createCipher('des-cbc', '12345678'); + o.final(); + o.update(''); +}, expectNotInitialized); + +assert.throws(function() { + var o = crypto.createDecipher('des-cbc', '12345678'); + o.final(); + o.update(''); +}, expectNotInitialized); \ No newline at end of file