Skip to content

Commit

Permalink
crypto: throw descriptive error when crypto object is not initialized
Browse files Browse the repository at this point in the history
  • Loading branch information
bnoordhuis committed Jul 30, 2011
1 parent d3d8f1b commit b31539a
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 28 deletions.
75 changes: 47 additions & 28 deletions src/node_crypto.cc
Expand Up @@ -63,6 +63,13 @@ static Persistent<String> ext_key_usage_symbol;

static Persistent<FunctionTemplate> secure_context_constructor;


Handle<Value> NotInitializedException() {
HandleScope scope;
return ThrowException(Exception::Error(String::New("Not initialized")));
}


void SecureContext::Initialize(Handle<Object> target) {
HandleScope scope;

Expand Down Expand Up @@ -1801,10 +1808,13 @@ class Cipher : public ObjectWrap {
}

static Handle<Value> CipherUpdate(const Arguments& args) {
Cipher *cipher = ObjectWrap::Unwrap<Cipher>(args.This());

HandleScope scope;

Cipher *cipher = ObjectWrap::Unwrap<Cipher>(args.This());
if (!cipher->initialised_) {
return NotInitializedException();
}

ASSERT_IS_STRING_OR_BUFFER(args[0]);

enum encoding enc = ParseEncoding(args[1]);
Expand Down Expand Up @@ -2185,6 +2195,9 @@ class Decipher : public ObjectWrap {
HandleScope scope;

Decipher *cipher = ObjectWrap::Unwrap<Decipher>(args.This());
if (!cipher->initialised_) {
return NotInitializedException();
}

ASSERT_IS_STRING_OR_BUFFER(args[0]);

Expand Down Expand Up @@ -2529,10 +2542,13 @@ class Hmac : public ObjectWrap {
}

static Handle<Value> HmacUpdate(const Arguments& args) {
Hmac *hmac = ObjectWrap::Unwrap<Hmac>(args.This());

HandleScope scope;

Hmac *hmac = ObjectWrap::Unwrap<Hmac>(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);
Expand Down Expand Up @@ -2684,6 +2700,9 @@ class Hash : public ObjectWrap {
HandleScope scope;

Hash *hash = ObjectWrap::Unwrap<Hash>(args.This());
if (!hash->initialised_) {
return NotInitializedException();
}

ASSERT_IS_STRING_OR_BUFFER(args[0]);
enum encoding enc = ParseEncoding(args[1]);
Expand Down Expand Up @@ -2721,9 +2740,8 @@ class Hash : public ObjectWrap {
HandleScope scope;

Hash *hash = ObjectWrap::Unwrap<Hash>(args.This());

if (!hash->initialised_) {
return ThrowException(Exception::Error(String::New("Not initialized")));
return NotInitializedException();
}

unsigned char md_value[EVP_MAX_MD_SIZE];
Expand Down Expand Up @@ -2877,10 +2895,13 @@ class Sign : public ObjectWrap {
}

static Handle<Value> SignUpdate(const Arguments& args) {
Sign *sign = ObjectWrap::Unwrap<Sign>(args.This());

HandleScope scope;

Sign *sign = ObjectWrap::Unwrap<Sign>(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);
Expand Down Expand Up @@ -3332,14 +3353,13 @@ class DiffieHellman : public ObjectWrap {
}

static Handle<Value> GenerateKeys(const Arguments& args) {
HandleScope scope;

DiffieHellman* diffieHellman =
ObjectWrap::Unwrap<DiffieHellman>(args.This());

HandleScope scope;

if (!diffieHellman->initialised_) {
return ThrowException(Exception::Error(
String::New("Not initialized")));
return NotInitializedException();
}

if (!DH_generate_key(diffieHellman->dh)) {
Expand All @@ -3365,13 +3385,13 @@ class DiffieHellman : public ObjectWrap {
}

static Handle<Value> GetPrime(const Arguments& args) {
HandleScope scope;

DiffieHellman* diffieHellman =
ObjectWrap::Unwrap<DiffieHellman>(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);
Expand All @@ -3392,13 +3412,13 @@ class DiffieHellman : public ObjectWrap {
}

static Handle<Value> GetGenerator(const Arguments& args) {
HandleScope scope;

DiffieHellman* diffieHellman =
ObjectWrap::Unwrap<DiffieHellman>(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);
Expand All @@ -3419,13 +3439,13 @@ class DiffieHellman : public ObjectWrap {
}

static Handle<Value> GetPublicKey(const Arguments& args) {
HandleScope scope;

DiffieHellman* diffieHellman =
ObjectWrap::Unwrap<DiffieHellman>(args.This());

HandleScope scope;

if (!diffieHellman->initialised_) {
return ThrowException(Exception::Error(String::New("Not initialized")));
return NotInitializedException();
}

if (diffieHellman->dh->pub_key == NULL) {
Expand All @@ -3452,13 +3472,13 @@ class DiffieHellman : public ObjectWrap {
}

static Handle<Value> GetPrivateKey(const Arguments& args) {
HandleScope scope;

DiffieHellman* diffieHellman =
ObjectWrap::Unwrap<DiffieHellman>(args.This());

HandleScope scope;

if (!diffieHellman->initialised_) {
return ThrowException(Exception::Error(String::New("Not initialized")));
return NotInitializedException();
}

if (diffieHellman->dh->priv_key == NULL) {
Expand Down Expand Up @@ -3491,7 +3511,7 @@ class DiffieHellman : public ObjectWrap {
ObjectWrap::Unwrap<DiffieHellman>(args.This());

if (!diffieHellman->initialised_) {
return ThrowException(Exception::Error(String::New("Not initialized")));
return NotInitializedException();
}

BIGNUM* key = 0;
Expand Down Expand Up @@ -3573,7 +3593,7 @@ class DiffieHellman : public ObjectWrap {
ObjectWrap::Unwrap<DiffieHellman>(args.This());

if (!diffieHellman->initialised_) {
return ThrowException(Exception::Error(String::New("Not initialized")));
return NotInitializedException();
}

if (args.Length() == 0) {
Expand Down Expand Up @@ -3618,8 +3638,7 @@ class DiffieHellman : public ObjectWrap {
ObjectWrap::Unwrap<DiffieHellman>(args.This());

if (!diffieHellman->initialised_) {
return ThrowException(Exception::Error(
String::New("Not initialized")));
return NotInitializedException();
}

if (args.Length() == 0) {
Expand Down
28 changes: 28 additions & 0 deletions test/simple/test-crypto.js
Expand Up @@ -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);

0 comments on commit b31539a

Please sign in to comment.