diff --git a/src/node_crypto.cc b/src/node_crypto.cc index abace371c3..dc3b2cd87b 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -59,6 +59,7 @@ Handle SecureContext::New(const Arguments& args) { return args.This(); } + Handle SecureContext::Init(const Arguments& args) { HandleScope scope; @@ -98,8 +99,6 @@ Handle SecureContext::Init(const Arguments& args) { method = TLSv1_client_method(); } - - sc->pCtx = SSL_CTX_new(method); // Enable session caching? SSL_CTX_set_session_cache_mode(sc->pCtx, SSL_SESS_CACHE_SERVER); @@ -110,6 +109,7 @@ Handle SecureContext::Init(const Arguments& args) { return True(); } + Handle SecureContext::SetKey(const Arguments& args) { HandleScope scope; @@ -138,6 +138,7 @@ Handle SecureContext::SetKey(const Arguments& args) { return True(); } + Handle SecureContext::SetCert(const Arguments& args) { HandleScope scope; @@ -167,6 +168,7 @@ Handle SecureContext::SetCert(const Arguments& args) { return True(); } + Handle SecureContext::AddCACert(const Arguments& args) { HandleScope scope; @@ -228,7 +230,6 @@ Handle SecureContext::Close(const Arguments& args) { } - void SecureStream::Initialize(Handle target) { HandleScope scope; @@ -344,6 +345,7 @@ Handle SecureStream::ReadInject(const Arguments& args) { return scope.Close(Integer::New(bytes_written)); } + Handle SecureStream::ReadExtract(const Arguments& args) { HandleScope scope; @@ -408,6 +410,7 @@ Handle SecureStream::ReadExtract(const Arguments& args) { return scope.Close(Integer::New(bytes_read)); } + Handle SecureStream::ReadPending(const Arguments& args) { HandleScope scope; @@ -416,6 +419,7 @@ Handle SecureStream::ReadPending(const Arguments& args) { return scope.Close(Integer::New(bytes_pending)); } + Handle SecureStream::WriteCanExtract(const Arguments& args) { HandleScope scope; @@ -424,6 +428,7 @@ Handle SecureStream::WriteCanExtract(const Arguments& args) { return scope.Close(Integer::New(bytes_pending)); } + Handle SecureStream::WriteExtract(const Arguments& args) { HandleScope scope; @@ -539,6 +544,7 @@ Handle SecureStream::GetPeerCertificate(const Arguments& args) { return scope.Close(info); } + Handle SecureStream::Shutdown(const Arguments& args) { HandleScope scope; @@ -551,6 +557,7 @@ Handle SecureStream::Shutdown(const Arguments& args) { return False(); } + Handle SecureStream::IsInitFinished(const Arguments& args) { HandleScope scope; @@ -593,6 +600,7 @@ Handle SecureStream::VerifyPeer(const Arguments& args) { return False(); } + Handle SecureStream::GetCurrentCipher(const Arguments& args) { HandleScope scope; @@ -623,8 +631,10 @@ Handle SecureStream::Close(const Arguments& args) { } -void hex_encode(unsigned char *md_value, int md_len, char** md_hexdigest, - int* md_hex_len) { +static void HexEncode(unsigned char *md_value, + int md_len, + char** md_hexdigest, + int* md_hex_len) { *md_hex_len = (2*(md_len)); *md_hexdigest = (char *) malloc(*md_hex_len + 1); for (int i = 0; i < md_len; i++) { @@ -634,8 +644,11 @@ void hex_encode(unsigned char *md_value, int md_len, char** md_hexdigest, #define hex2i(c) ((c) <= '9' ? ((c) - '0') : (c) <= 'Z' ? ((c) - 'A' + 10) \ : ((c) - 'a' + 10)) -void hex_decode(unsigned char *input, int length, char** buf64, - int* buf64_len) { + +static void HexDecode(unsigned char *input, + int length, + char** buf64, + int* buf64_len) { *buf64_len = (length/2); *buf64 = (char*) malloc(length/2 + 1); char *b = *buf64; @@ -644,8 +657,8 @@ void hex_decode(unsigned char *input, int length, char** buf64, } } -void base64(unsigned char *input, int length, char** buf64, int* buf64_len) -{ + +void base64(unsigned char *input, int length, char** buf64, int* buf64_len) { BIO *bmem, *b64; BUF_MEM *bptr; @@ -664,11 +677,13 @@ void base64(unsigned char *input, int length, char** buf64, int* buf64_len) b[bptr->length] = 0; BIO_free_all(b64); - } -void *unbase64(unsigned char *input, int length, char** buffer, int* buffer_len) -{ + +void *unbase64(unsigned char *input, + int length, + char** buffer, + int* buffer_len) { BIO *b64, *bmem; *buffer = (char *)malloc(length); memset(*buffer, 0, length); @@ -680,7 +695,6 @@ void *unbase64(unsigned char *input, int length, char** buffer, int* buffer_len) *buffer_len = BIO_read(bmem, *buffer, length); BIO_free_all(bmem); - } @@ -736,11 +750,13 @@ static int LengthWithoutIncompleteUtf8(char* buffer, int len) { return 0; } + // local decrypt final without strict padding check // to work with php mcrypt // see http://www.mail-archive.com/openssl-dev@openssl.org/msg19927.html -int local_EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl) -{ +int local_EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, + unsigned char *out, + int *outl) { int i,b; int n; @@ -792,9 +808,7 @@ int local_EVP_DecryptFinal_ex(EVP_CIPHER_CTX *ctx, unsigned char *out, int *outl class Cipher : public ObjectWrap { public: - static void - Initialize (v8::Handle target) - { + static void Initialize (v8::Handle target) { HandleScope scope; Local t = FunctionTemplate::New(New); @@ -809,8 +823,8 @@ class Cipher : public ObjectWrap { target->Set(String::NewSymbol("Cipher"), t->GetFunction()); } - bool CipherInit(char* cipherType, char* key_buf, int key_buf_len) - { + + bool CipherInit(char* cipherType, char* key_buf, int key_buf_len) { cipher = EVP_get_cipherbyname(cipherType); if(!cipher) { fprintf(stderr, "node-crypto : Unknown cipher %s\n", cipherType); @@ -827,13 +841,16 @@ class Cipher : public ObjectWrap { EVP_CIPHER_CTX_cleanup(&ctx); return false; } - initialised = true; + initialised_ = true; return true; } - bool CipherInitIv(char* cipherType, char* key, int key_len, char *iv, int iv_len) - { + bool CipherInitIv(char* cipherType, + char* key, + int key_len, + char *iv, + int iv_len) { cipher = EVP_get_cipherbyname(cipherType); if(!cipher) { fprintf(stderr, "node-crypto : Unknown cipher %s\n", cipherType); @@ -850,37 +867,33 @@ class Cipher : public ObjectWrap { EVP_CIPHER_CTX_cleanup(&ctx); return false; } - initialised = true; + initialised_ = true; return true; } int CipherUpdate(char* data, int len, unsigned char** out, int* out_len) { - if (!initialised) - return 0; + if (!initialised_) return 0; *out_len=len+EVP_CIPHER_CTX_block_size(&ctx); *out=(unsigned char*)malloc(*out_len); - + EVP_CipherUpdate(&ctx, *out, out_len, (unsigned char*)data, len); return 1; } int CipherFinal(unsigned char** out, int *out_len) { - if (!initialised) - return 0; + if (!initialised_) return 0; *out = (unsigned char*) malloc(EVP_CIPHER_CTX_block_size(&ctx)); EVP_CipherFinal(&ctx,*out,out_len); EVP_CIPHER_CTX_cleanup(&ctx); - initialised = false; + initialised_ = false; return 1; } protected: - static Handle - New (const Arguments& args) - { + static Handle New (const Arguments& args) { HandleScope scope; Cipher *cipher = new Cipher(); @@ -888,18 +901,16 @@ class Cipher : public ObjectWrap { return args.This(); } - static Handle - CipherInit(const Arguments& args) { - Cipher *cipher = ObjectWrap::Unwrap(args.This()); - + static Handle CipherInit(const Arguments& args) { HandleScope scope; + Cipher *cipher = ObjectWrap::Unwrap(args.This()); + cipher->incomplete_base64=NULL; if (args.Length() <= 1 || !args[0]->IsString() || !args[1]->IsString()) { return ThrowException(String::New("Must give cipher-type, key")); } - ssize_t key_buf_len = DecodeBytes(args[1], BINARY); @@ -907,11 +918,11 @@ class Cipher : public ObjectWrap { Local exception = Exception::TypeError(String::New("Bad argument")); return ThrowException(exception); } - + char* key_buf = new char[key_buf_len]; ssize_t key_written = DecodeWrite(key_buf, key_buf_len, args[1], BINARY); assert(key_written == key_buf_len); - + String::Utf8Value cipherType(args[0]->ToString()); bool r = cipher->CipherInit(*cipherType, key_buf, key_buf_len); @@ -919,8 +930,8 @@ class Cipher : public ObjectWrap { return args.This(); } - static Handle - CipherInitIv(const Arguments& args) { + + static Handle CipherInitIv(const Arguments& args) { Cipher *cipher = ObjectWrap::Unwrap(args.This()); HandleScope scope; @@ -936,7 +947,7 @@ class Cipher : public ObjectWrap { Local exception = Exception::TypeError(String::New("Bad argument")); return ThrowException(exception); } - + ssize_t iv_len = DecodeBytes(args[2], BINARY); if (iv_len < 0) { @@ -947,7 +958,7 @@ class Cipher : public ObjectWrap { char* key_buf = new char[key_len]; ssize_t key_written = DecodeWrite(key_buf, key_len, args[1], BINARY); assert(key_written == key_len); - + char* iv_buf = new char[iv_len]; ssize_t iv_written = DecodeWrite(iv_buf, iv_len, args[2], BINARY); assert(iv_written == iv_len); @@ -960,8 +971,7 @@ class Cipher : public ObjectWrap { } - static Handle - CipherUpdate(const Arguments& args) { + static Handle CipherUpdate(const Arguments& args) { Cipher *cipher = ObjectWrap::Unwrap(args.This()); HandleScope scope; @@ -981,7 +991,7 @@ class Cipher : public ObjectWrap { unsigned char *out=0; int out_len=0; int r = cipher->CipherUpdate(buf, len,&out,&out_len); - + Local outString; if (out_len==0) outString=String::New(""); else { @@ -994,7 +1004,7 @@ class Cipher : public ObjectWrap { String::Utf8Value encoding(args[2]->ToString()); if (strcasecmp(*encoding, "hex") == 0) { // Hex encoding - hex_encode(out, out_len, &out_hexdigest, &out_hex_len); + HexEncode(out, out_len, &out_hexdigest, &out_hex_len); outString = Encode(out_hexdigest, out_hex_len, BINARY); free(out_hexdigest); } else if (strcasecmp(*encoding, "base64") == 0) { @@ -1035,8 +1045,7 @@ class Cipher : public ObjectWrap { return scope.Close(outString); } - static Handle - CipherFinal(const Arguments& args) { + static Handle CipherFinal(const Arguments& args) { Cipher *cipher = ObjectWrap::Unwrap(args.This()); HandleScope scope; @@ -1060,7 +1069,7 @@ class Cipher : public ObjectWrap { String::Utf8Value encoding(args[0]->ToString()); if (strcasecmp(*encoding, "hex") == 0) { // Hex encoding - hex_encode(out_value, out_len, &out_hexdigest, &out_hex_len); + HexEncode(out_value, out_len, &out_hexdigest, &out_hex_len); outString = Encode(out_hexdigest, out_hex_len, BINARY); free(out_hexdigest); } else if (strcasecmp(*encoding, "base64") == 0) { @@ -1070,18 +1079,17 @@ class Cipher : public ObjectWrap { } else if (strcasecmp(*encoding, "binary") == 0) { outString = Encode(out_value, out_len, BINARY); } else { - fprintf(stderr, "node-crypto : Cipher .final encoding " - "can be binary, hex or base64\n"); + fprintf(stderr, "node-crypto : Cipher .final encoding " + "can be binary, hex or base64\n"); } } free(out_value); return scope.Close(outString); - } - Cipher () : ObjectWrap () + Cipher () : ObjectWrap () { - initialised = false; + initialised_ = false; } ~Cipher () @@ -1092,7 +1100,7 @@ class Cipher : public ObjectWrap { EVP_CIPHER_CTX ctx; const EVP_CIPHER *cipher; - bool initialised; + bool initialised_; char* incomplete_base64; int incomplete_base64_len; @@ -1120,64 +1128,80 @@ class Decipher : public ObjectWrap { target->Set(String::NewSymbol("Decipher"), t->GetFunction()); } - bool DecipherInit(char* cipherType, char* key_buf, int key_buf_len) - { - cipher = EVP_get_cipherbyname(cipherType); - if(!cipher) { + bool DecipherInit(char* cipherType, char* key_buf, int key_buf_len) { + cipher_ = EVP_get_cipherbyname(cipherType); + + if(!cipher_) { fprintf(stderr, "node-crypto : Unknown cipher %s\n", cipherType); return false; } unsigned char key[EVP_MAX_KEY_LENGTH],iv[EVP_MAX_IV_LENGTH]; - int key_len = EVP_BytesToKey(cipher, EVP_md5(), NULL, (unsigned char*) key_buf, key_buf_len, 1, key, iv); + int key_len = EVP_BytesToKey(cipher_, + EVP_md5(), + NULL, + (unsigned char*)(key_buf), + key_buf_len, + 1, + key, + iv); EVP_CIPHER_CTX_init(&ctx); - EVP_CipherInit(&ctx,cipher,(unsigned char *)key,(unsigned char *)iv, false); + EVP_CipherInit(&ctx, + cipher_, + (unsigned char*)(key), + (unsigned char *)(iv), + false); if (!EVP_CIPHER_CTX_set_key_length(&ctx,key_len)) { fprintf(stderr, "node-crypto : Invalid key length %d\n", key_len); EVP_CIPHER_CTX_cleanup(&ctx); return false; } - initialised = true; + initialised_ = true; return true; } - bool DecipherInitIv(char* cipherType, char* key, int key_len, char *iv, int iv_len) - { - cipher = EVP_get_cipherbyname(cipherType); - if(!cipher) { + bool DecipherInitIv(char* cipherType, + char* key, + int key_len, + char *iv, + int iv_len) { + cipher_ = EVP_get_cipherbyname(cipherType); + if(!cipher_) { fprintf(stderr, "node-crypto : Unknown cipher %s\n", cipherType); return false; } - if (EVP_CIPHER_iv_length(cipher)!=iv_len) { + if (EVP_CIPHER_iv_length(cipher_) != iv_len) { fprintf(stderr, "node-crypto : Invalid IV length %d\n", iv_len); return false; } EVP_CIPHER_CTX_init(&ctx); - EVP_CipherInit(&ctx,cipher,(unsigned char *)key,(unsigned char *)iv, false); + EVP_CipherInit(&ctx, + cipher_, + (unsigned char*)(key), + (unsigned char *)(iv), + false); if (!EVP_CIPHER_CTX_set_key_length(&ctx,key_len)) { fprintf(stderr, "node-crypto : Invalid key length %d\n", key_len); EVP_CIPHER_CTX_cleanup(&ctx); return false; } - initialised = true; + initialised_ = true; return true; } int DecipherUpdate(char* data, int len, unsigned char** out, int* out_len) { - if (!initialised) - return 0; + if (!initialised_) return 0; *out_len=len+EVP_CIPHER_CTX_block_size(&ctx); *out=(unsigned char*)malloc(*out_len); - + EVP_CipherUpdate(&ctx, *out, out_len, (unsigned char*)data, len); return 1; } int DecipherFinal(unsigned char** out, int *out_len, bool tolerate_padding) { - if (!initialised) - return 0; + if (!initialised_) return 0; *out = (unsigned char*) malloc(EVP_CIPHER_CTX_block_size(&ctx)); if (tolerate_padding) { local_EVP_DecryptFinal_ex(&ctx,*out,out_len); @@ -1185,16 +1209,14 @@ class Decipher : public ObjectWrap { EVP_CipherFinal(&ctx,*out,out_len); } EVP_CIPHER_CTX_cleanup(&ctx); - initialised = false; + initialised_ = false; return 1; } protected: - static Handle - New (const Arguments& args) - { + static Handle New (const Arguments& args) { HandleScope scope; Decipher *cipher = new Decipher(); @@ -1202,8 +1224,7 @@ class Decipher : public ObjectWrap { return args.This(); } - static Handle - DecipherInit(const Arguments& args) { + static Handle DecipherInit(const Arguments& args) { Decipher *cipher = ObjectWrap::Unwrap(args.This()); HandleScope scope; @@ -1221,11 +1242,11 @@ class Decipher : public ObjectWrap { Local exception = Exception::TypeError(String::New("Bad argument")); return ThrowException(exception); } - + char* key_buf = new char[key_len]; ssize_t key_written = DecodeWrite(key_buf, key_len, args[1], BINARY); assert(key_written == key_len); - + String::Utf8Value cipherType(args[0]->ToString()); bool r = cipher->DecipherInit(*cipherType, key_buf,key_len); @@ -1233,8 +1254,7 @@ class Decipher : public ObjectWrap { return args.This(); } - static Handle - DecipherInitIv(const Arguments& args) { + static Handle DecipherInitIv(const Arguments& args) { Decipher *cipher = ObjectWrap::Unwrap(args.This()); HandleScope scope; @@ -1252,7 +1272,7 @@ class Decipher : public ObjectWrap { Local exception = Exception::TypeError(String::New("Bad argument")); return ThrowException(exception); } - + ssize_t iv_len = DecodeBytes(args[2], BINARY); if (iv_len < 0) { @@ -1263,7 +1283,7 @@ class Decipher : public ObjectWrap { char* key_buf = new char[key_len]; ssize_t key_written = DecodeWrite(key_buf, key_len, args[1], BINARY); assert(key_written == key_len); - + char* iv_buf = new char[iv_len]; ssize_t iv_written = DecodeWrite(iv_buf, iv_len, args[2], BINARY); assert(iv_written == iv_len); @@ -1275,58 +1295,58 @@ class Decipher : public ObjectWrap { return args.This(); } - static Handle - DecipherUpdate(const Arguments& args) { - Decipher *cipher = ObjectWrap::Unwrap(args.This()); - + static Handle DecipherUpdate(const Arguments& args) { HandleScope scope; + Decipher *cipher = ObjectWrap::Unwrap(args.This()); + ssize_t len = DecodeBytes(args[0], BINARY); char* buf = new char[len]; ssize_t written = DecodeWrite(buf, len, args[0], BINARY); char* ciphertext; int ciphertext_len; - if (args.Length() <= 1 || !args[1]->IsString()) { // Binary - do nothing } else { String::Utf8Value encoding(args[1]->ToString()); if (strcasecmp(*encoding, "hex") == 0) { - // Hex encoding - // Do we have a previous hex carry over? - if (cipher->incomplete_hex_flag) { - char* complete_hex = (char*)malloc(len+2); - memcpy(complete_hex, &cipher->incomplete_hex, 1); - memcpy(complete_hex+1, buf, len); - free(buf); - buf = complete_hex; - len += 1; - } - // Do we have an incomplete hex stream? - if ((len>0) && (len % 2 !=0)) { - len--; - cipher->incomplete_hex=buf[len]; - cipher->incomplete_hex_flag=true; - buf[len]=0; - } - hex_decode((unsigned char*)buf, len, (char **)&ciphertext, &ciphertext_len); + // Hex encoding + // Do we have a previous hex carry over? + if (cipher->incomplete_hex_flag) { + char* complete_hex = (char*)malloc(len+2); + memcpy(complete_hex, &cipher->incomplete_hex, 1); + memcpy(complete_hex+1, buf, len); + free(buf); + buf = complete_hex; + len += 1; + } + // Do we have an incomplete hex stream? + if ((len>0) && (len % 2 !=0)) { + len--; + cipher->incomplete_hex=buf[len]; + cipher->incomplete_hex_flag=true; + buf[len]=0; + } + HexDecode((unsigned char*)buf, len, (char **)&ciphertext, &ciphertext_len); free(buf); - buf = ciphertext; - len = ciphertext_len; + buf = ciphertext; + len = ciphertext_len; + } else if (strcasecmp(*encoding, "base64") == 0) { unbase64((unsigned char*)buf, len, (char **)&ciphertext, &ciphertext_len); free(buf); - buf = ciphertext; - len = ciphertext_len; + buf = ciphertext; + len = ciphertext_len; + } else if (strcasecmp(*encoding, "binary") == 0) { // Binary - do nothing + } else { - fprintf(stderr, "node-crypto : Decipher .update encoding " - "can be binary, hex or base64\n"); + fprintf(stderr, "node-crypto : Decipher .update encoding " + "can be binary, hex or base64\n"); } - } unsigned char *out=0; @@ -1341,24 +1361,24 @@ class Decipher : public ObjectWrap { } else { enum encoding enc = ParseEncoding(args[2]); if (enc == UTF8) { - // See if we have any overhang from last utf8 partial ending - if (cipher->incomplete_utf8!=NULL) { - char* complete_out = (char *)malloc(cipher->incomplete_utf8_len + out_len); - memcpy(complete_out, cipher->incomplete_utf8, cipher->incomplete_utf8_len); - memcpy((char *)complete_out+cipher->incomplete_utf8_len, out, out_len); - free(out); - free(cipher->incomplete_utf8); - cipher->incomplete_utf8=NULL; - out = (unsigned char*)complete_out; - out_len += cipher->incomplete_utf8_len; - } - // Check to see if we have a complete utf8 stream - int utf8_len = LengthWithoutIncompleteUtf8((char *)out, out_len); - if (utf8_lenincomplete_utf8_len = out_len-utf8_len; + // See if we have any overhang from last utf8 partial ending + if (cipher->incomplete_utf8!=NULL) { + char* complete_out = (char *)malloc(cipher->incomplete_utf8_len + out_len); + memcpy(complete_out, cipher->incomplete_utf8, cipher->incomplete_utf8_len); + memcpy((char *)complete_out+cipher->incomplete_utf8_len, out, out_len); + free(out); + free(cipher->incomplete_utf8); + cipher->incomplete_utf8=NULL; + out = (unsigned char*)complete_out; + out_len += cipher->incomplete_utf8_len; + } + // Check to see if we have a complete utf8 stream + int utf8_len = LengthWithoutIncompleteUtf8((char *)out, out_len); + if (utf8_lenincomplete_utf8_len = out_len-utf8_len; cipher->incomplete_utf8 = (unsigned char *)malloc(cipher->incomplete_utf8_len+1); memcpy(cipher->incomplete_utf8, &out[utf8_len], cipher->incomplete_utf8_len); - } + } outString = Encode(out, utf8_len, enc); } else { outString = Encode(out, out_len, enc); @@ -1371,12 +1391,11 @@ class Decipher : public ObjectWrap { } - static Handle - DecipherFinal(const Arguments& args) { - Decipher *cipher = ObjectWrap::Unwrap(args.This()); - + static Handle DecipherFinal(const Arguments& args) { HandleScope scope; + Decipher *cipher = ObjectWrap::Unwrap(args.This()); + unsigned char* out_value; int out_len; char* out_hexdigest; @@ -1395,29 +1414,27 @@ class Decipher : public ObjectWrap { } else { enum encoding enc = ParseEncoding(args[0]); if (enc == UTF8) { - // See if we have any overhang from last utf8 partial ending - if (cipher->incomplete_utf8!=NULL) { - char* complete_out = (char *)malloc(cipher->incomplete_utf8_len + out_len); - memcpy(complete_out, cipher->incomplete_utf8, cipher->incomplete_utf8_len); - memcpy((char *)complete_out+cipher->incomplete_utf8_len, out_value, out_len); - free(cipher->incomplete_utf8); - cipher->incomplete_utf8=NULL; - outString = Encode(complete_out, cipher->incomplete_utf8_len+out_len, enc); - free(complete_out); - } else { - outString = Encode(out_value, out_len, enc); - } + // See if we have any overhang from last utf8 partial ending + if (cipher->incomplete_utf8!=NULL) { + char* complete_out = (char *)malloc(cipher->incomplete_utf8_len + out_len); + memcpy(complete_out, cipher->incomplete_utf8, cipher->incomplete_utf8_len); + memcpy((char *)complete_out+cipher->incomplete_utf8_len, out_value, out_len); + free(cipher->incomplete_utf8); + cipher->incomplete_utf8=NULL; + outString = Encode(complete_out, cipher->incomplete_utf8_len+out_len, enc); + free(complete_out); + } else { + outString = Encode(out_value, out_len, enc); + } } else { - outString = Encode(out_value, out_len, enc); + outString = Encode(out_value, out_len, enc); } } free(out_value); return scope.Close(outString); - } - static Handle - DecipherFinalTolerate(const Arguments& args) { + static Handle DecipherFinalTolerate(const Arguments& args) { Decipher *cipher = ObjectWrap::Unwrap(args.This()); HandleScope scope; @@ -1440,41 +1457,37 @@ class Decipher : public ObjectWrap { } else { enum encoding enc = ParseEncoding(args[0]); if (enc == UTF8) { - // See if we have any overhang from last utf8 partial ending - if (cipher->incomplete_utf8!=NULL) { - char* complete_out = (char *)malloc(cipher->incomplete_utf8_len + out_len); - memcpy(complete_out, cipher->incomplete_utf8, cipher->incomplete_utf8_len); - memcpy((char *)complete_out+cipher->incomplete_utf8_len, out_value, out_len); - free(cipher->incomplete_utf8); - cipher->incomplete_utf8=NULL; - outString = Encode(complete_out, cipher->incomplete_utf8_len+out_len, enc); - free(complete_out); - } else { - outString = Encode(out_value, out_len, enc); - } + // See if we have any overhang from last utf8 partial ending + if (cipher->incomplete_utf8!=NULL) { + char* complete_out = (char *)malloc(cipher->incomplete_utf8_len + out_len); + memcpy(complete_out, cipher->incomplete_utf8, cipher->incomplete_utf8_len); + memcpy((char *)complete_out+cipher->incomplete_utf8_len, out_value, out_len); + free(cipher->incomplete_utf8); + cipher->incomplete_utf8=NULL; + outString = Encode(complete_out, cipher->incomplete_utf8_len+out_len, enc); + free(complete_out); + } else { + outString = Encode(out_value, out_len, enc); + } } else { - outString = Encode(out_value, out_len, enc); + outString = Encode(out_value, out_len, enc); } } free(out_value); return scope.Close(outString); - } - Decipher () : ObjectWrap () - { - initialised = false; + Decipher () : ObjectWrap () { + initialised_ = false; } - ~Decipher () - { - } + ~Decipher () { } private: EVP_CIPHER_CTX ctx; - const EVP_CIPHER *cipher; - bool initialised; + const EVP_CIPHER *cipher_; + bool initialised_; unsigned char* incomplete_utf8; int incomplete_utf8_len; char incomplete_hex; @@ -1486,9 +1499,7 @@ class Decipher : public ObjectWrap { class Hmac : public ObjectWrap { public: - static void - Initialize (v8::Handle target) - { + static void Initialize (v8::Handle target) { HandleScope scope; Local t = FunctionTemplate::New(New); @@ -1502,8 +1513,7 @@ class Hmac : public ObjectWrap { target->Set(String::NewSymbol("Hmac"), t->GetFunction()); } - bool HmacInit(char* hashType, char* key, int key_len) - { + bool HmacInit(char* hashType, char* key, int key_len) { md = EVP_get_digestbyname(hashType); if(!md) { fprintf(stderr, "node-crypto : Unknown message digest %s\n", hashType); @@ -1511,34 +1521,30 @@ class Hmac : public ObjectWrap { } HMAC_CTX_init(&ctx); HMAC_Init(&ctx, key, key_len, md); - initialised = true; + initialised_ = true; return true; - + } int HmacUpdate(char* data, int len) { - if (!initialised) - return 0; + if (!initialised_) return 0; HMAC_Update(&ctx, (unsigned char*)data, len); return 1; } int HmacDigest(unsigned char** md_value, unsigned int *md_len) { - if (!initialised) - return 0; + if (!initialised_) return 0; *md_value = (unsigned char*) malloc(EVP_MAX_MD_SIZE); HMAC_Final(&ctx, *md_value, md_len); HMAC_CTX_cleanup(&ctx); - initialised = false; + initialised_ = false; return 1; } protected: - static Handle - New (const Arguments& args) - { + static Handle New (const Arguments& args) { HandleScope scope; Hmac *hmac = new Hmac(); @@ -1546,8 +1552,7 @@ class Hmac : public ObjectWrap { return args.This(); } - static Handle - HmacInit(const Arguments& args) { + static Handle HmacInit(const Arguments& args) { Hmac *hmac = ObjectWrap::Unwrap(args.This()); HandleScope scope; @@ -1574,8 +1579,7 @@ class Hmac : public ObjectWrap { return args.This(); } - static Handle - HmacUpdate(const Arguments& args) { + static Handle HmacUpdate(const Arguments& args) { Hmac *hmac = ObjectWrap::Unwrap(args.This()); HandleScope scope; @@ -1597,8 +1601,7 @@ class Hmac : public ObjectWrap { return args.This(); } - static Handle - HmacDigest(const Arguments& args) { + static Handle HmacDigest(const Arguments& args) { Hmac *hmac = ObjectWrap::Unwrap(args.This()); HandleScope scope; @@ -1622,7 +1625,7 @@ class Hmac : public ObjectWrap { String::Utf8Value encoding(args[0]->ToString()); if (strcasecmp(*encoding, "hex") == 0) { // Hex encoding - hex_encode(md_value, md_len, &md_hexdigest, &md_hex_len); + HexEncode(md_value, md_len, &md_hexdigest, &md_hex_len); outString = Encode(md_hexdigest, md_hex_len, BINARY); free(md_hexdigest); } else if (strcasecmp(*encoding, "base64") == 0) { @@ -1632,38 +1635,31 @@ class Hmac : public ObjectWrap { } else if (strcasecmp(*encoding, "binary") == 0) { outString = Encode(md_value, md_len, BINARY); } else { - fprintf(stderr, "node-crypto : Hmac .digest encoding " - "can be binary, hex or base64\n"); + fprintf(stderr, "node-crypto : Hmac .digest encoding " + "can be binary, hex or base64\n"); } } free(md_value); return scope.Close(outString); - } - Hmac () : ObjectWrap () - { - initialised = false; + Hmac () : ObjectWrap () { + initialised_ = false; } - ~Hmac () - { - } + ~Hmac () { } private: HMAC_CTX ctx; const EVP_MD *md; - bool initialised; - + bool initialised_; }; class Hash : public ObjectWrap { public: - static void - Initialize (v8::Handle target) - { + static void Initialize (v8::Handle target) { HandleScope scope; Local t = FunctionTemplate::New(New); @@ -1676,8 +1672,7 @@ class Hash : public ObjectWrap { target->Set(String::NewSymbol("Hash"), t->GetFunction()); } - bool HashInit (const char* hashType) - { + bool HashInit (const char* hashType) { md = EVP_get_digestbyname(hashType); if(!md) { fprintf(stderr, "node-crypto : Unknown message digest %s\n", hashType); @@ -1685,24 +1680,22 @@ class Hash : public ObjectWrap { } EVP_MD_CTX_init(&mdctx); EVP_DigestInit_ex(&mdctx, md, NULL); - initialised = true; + initialised_ = true; return true; } int HashUpdate(char* data, int len) { - if (!initialised) - return 0; + if (!initialised_) return 0; EVP_DigestUpdate(&mdctx, data, len); return 1; } int HashDigest(unsigned char** md_value, unsigned int *md_len) { - if (!initialised) - return 0; + if (!initialised_) return 0; *md_value = (unsigned char*) malloc(EVP_MAX_MD_SIZE); EVP_DigestFinal_ex(&mdctx, *md_value, md_len); EVP_MD_CTX_cleanup(&mdctx); - initialised = false; + initialised_ = false; return 1; } @@ -1750,8 +1743,7 @@ class Hash : public ObjectWrap { return args.This(); } - static Handle - HashDigest(const Arguments& args) { + static Handle HashDigest(const Arguments& args) { Hash *hash = ObjectWrap::Unwrap(args.This()); HandleScope scope; @@ -1775,7 +1767,7 @@ class Hash : public ObjectWrap { String::Utf8Value encoding(args[0]->ToString()); if (strcasecmp(*encoding, "hex") == 0) { // Hex encoding - hex_encode(md_value, md_len, &md_hexdigest, &md_hex_len); + HexEncode(md_value, md_len, &md_hexdigest, &md_hex_len); outString = Encode(md_hexdigest, md_hex_len, BINARY); free(md_hexdigest); } else if (strcasecmp(*encoding, "base64") == 0) { @@ -1785,8 +1777,8 @@ class Hash : public ObjectWrap { } else if (strcasecmp(*encoding, "binary") == 0) { outString = Encode(md_value, md_len, BINARY); } else { - fprintf(stderr, "node-crypto : Hash .digest encoding " - "can be binary, hex or base64\n"); + fprintf(stderr, "node-crypto : Hash .digest encoding " + "can be binary, hex or base64\n"); } } free(md_value); @@ -1794,28 +1786,23 @@ class Hash : public ObjectWrap { } - Hash () : ObjectWrap () - { - initialised = false; + Hash () : ObjectWrap () { + initialised_ = false; } - ~Hash () - { - } + ~Hash () { } private: EVP_MD_CTX mdctx; const EVP_MD *md; - bool initialised; - + bool initialised_; }; class Sign : public ObjectWrap { public: static void - Initialize (v8::Handle target) - { + Initialize (v8::Handle target) { HandleScope scope; Local t = FunctionTemplate::New(New); @@ -1829,8 +1816,7 @@ class Sign : public ObjectWrap { target->Set(String::NewSymbol("Sign"), t->GetFunction()); } - bool SignInit (const char* signType) - { + bool SignInit (const char* signType) { md = EVP_get_digestbyname(signType); if(!md) { printf("Unknown message digest %s\n", signType); @@ -1838,35 +1824,34 @@ class Sign : public ObjectWrap { } EVP_MD_CTX_init(&mdctx); EVP_SignInit_ex(&mdctx, md, NULL); - initialised = true; + initialised_ = true; return true; - + } int SignUpdate(char* data, int len) { - if (!initialised) - return 0; + if (!initialised_) return 0; EVP_SignUpdate(&mdctx, data, len); return 1; } - int SignFinal(unsigned char** md_value, unsigned int *md_len, char* keyPem, int keyPemLen) { - if (!initialised) - return 0; + int SignFinal(unsigned char** md_value, + unsigned int *md_len, + char* keyPem, + int keyPemLen) { + if (!initialised_) return 0; BIO *bp = NULL; EVP_PKEY* pkey; - bp = BIO_new(BIO_s_mem()); - if(!BIO_write(bp, keyPem, keyPemLen)) - return 0; + bp = BIO_new(BIO_s_mem()); + if(!BIO_write(bp, keyPem, keyPemLen)) return 0; pkey = PEM_read_bio_PrivateKey( bp, NULL, NULL, NULL ); - if (pkey == NULL) - return 0; + if (pkey == NULL) return 0; EVP_SignFinal(&mdctx, *md_value, md_len, pkey); EVP_MD_CTX_cleanup(&mdctx); - initialised = false; + initialised_ = false; EVP_PKEY_free(pkey); BIO_free(bp); return 1; @@ -1875,9 +1860,7 @@ class Sign : public ObjectWrap { protected: - static Handle - New (const Arguments& args) - { + static Handle New (const Arguments& args) { HandleScope scope; Sign *sign = new Sign(); @@ -1886,12 +1869,11 @@ class Sign : public ObjectWrap { return args.This(); } - static Handle - SignInit(const Arguments& args) { - Sign *sign = ObjectWrap::Unwrap(args.This()); - + static Handle SignInit(const Arguments& args) { HandleScope scope; + Sign *sign = ObjectWrap::Unwrap(args.This()); + if (args.Length() == 0 || !args[0]->IsString()) { return ThrowException(String::New("Must give signtype string as argument")); } @@ -1903,8 +1885,7 @@ class Sign : public ObjectWrap { return args.This(); } - static Handle - SignUpdate(const Arguments& args) { + static Handle SignUpdate(const Arguments& args) { Sign *sign = ObjectWrap::Unwrap(args.This()); HandleScope scope; @@ -1926,8 +1907,7 @@ class Sign : public ObjectWrap { return args.This(); } - static Handle - SignFinal(const Arguments& args) { + static Handle SignFinal(const Arguments& args) { Sign *sign = ObjectWrap::Unwrap(args.This()); HandleScope scope; @@ -1952,7 +1932,6 @@ class Sign : public ObjectWrap { ssize_t written = DecodeWrite(buf, len, args[0], BINARY); assert(written == len); - int r = sign->SignFinal(&md_value, &md_len, buf, len); if (md_len == 0 || r == 0) { @@ -1966,7 +1945,7 @@ class Sign : public ObjectWrap { String::Utf8Value encoding(args[1]->ToString()); if (strcasecmp(*encoding, "hex") == 0) { // Hex encoding - hex_encode(md_value, md_len, &md_hexdigest, &md_hex_len); + HexEncode(md_value, md_len, &md_hexdigest, &md_hex_len); outString = Encode(md_hexdigest, md_hex_len, BINARY); free(md_hexdigest); } else if (strcasecmp(*encoding, "base64") == 0) { @@ -1976,37 +1955,31 @@ class Sign : public ObjectWrap { } else if (strcasecmp(*encoding, "binary") == 0) { outString = Encode(md_value, md_len, BINARY); } else { - outString = String::New(""); - fprintf(stderr, "node-crypto : Sign .sign encoding " - "can be binary, hex or base64\n"); + outString = String::New(""); + fprintf(stderr, "node-crypto : Sign .sign encoding " + "can be binary, hex or base64\n"); } } return scope.Close(outString); } - Sign () : ObjectWrap () - { - initialised = false; + Sign () : ObjectWrap () { + initialised_ = false; } - ~Sign () - { - } + ~Sign () { } private: EVP_MD_CTX mdctx; const EVP_MD *md; - bool initialised; - + bool initialised_; }; class Verify : public ObjectWrap { public: - static void - Initialize (v8::Handle target) - { + static void Initialize (v8::Handle target) { HandleScope scope; Local t = FunctionTemplate::New(New); @@ -2020,8 +1993,8 @@ class Verify : public ObjectWrap { target->Set(String::NewSymbol("Verify"), t->GetFunction()); } - bool VerifyInit (const char* verifyType) - { + + bool VerifyInit (const char* verifyType) { md = EVP_get_digestbyname(verifyType); if(!md) { fprintf(stderr, "node-crypto : Unknown message digest %s\n", verifyType); @@ -2029,37 +2002,33 @@ class Verify : public ObjectWrap { } EVP_MD_CTX_init(&mdctx); EVP_VerifyInit_ex(&mdctx, md, NULL); - initialised = true; + initialised_ = true; return true; - } + int VerifyUpdate(char* data, int len) { - if (!initialised) - return 0; + if (!initialised_) return 0; EVP_VerifyUpdate(&mdctx, data, len); return 1; } + int VerifyFinal(char* keyPem, int keyPemLen, unsigned char* sig, int siglen) { - if (!initialised) - return 0; + if (!initialised_) return 0; BIO *bp = NULL; EVP_PKEY* pkey; - X509 * x509; + X509 *x509; - bp = BIO_new(BIO_s_mem()); - if(!BIO_write(bp, keyPem, keyPemLen)) - return 0; + bp = BIO_new(BIO_s_mem()); + if(!BIO_write(bp, keyPem, keyPemLen)) return 0; x509 = PEM_read_bio_X509(bp, NULL, NULL, NULL ); - if (x509==NULL) - return 0; + if (x509==NULL) return 0; pkey=X509_get_pubkey(x509); - if (pkey==NULL) - return 0; + if (pkey==NULL) return 0; int r = EVP_VerifyFinal(&mdctx, sig, siglen, pkey); EVP_PKEY_free (pkey); @@ -2070,16 +2039,14 @@ class Verify : public ObjectWrap { X509_free(x509); BIO_free(bp); EVP_MD_CTX_cleanup(&mdctx); - initialised = false; + initialised_ = false; return r; } protected: - static Handle - New (const Arguments& args) - { + static Handle New (const Arguments& args) { HandleScope scope; Verify *verify = new Verify(); @@ -2088,8 +2055,8 @@ class Verify : public ObjectWrap { return args.This(); } - static Handle - VerifyInit(const Arguments& args) { + + static Handle VerifyInit(const Arguments& args) { Verify *verify = ObjectWrap::Unwrap(args.This()); HandleScope scope; @@ -2105,12 +2072,12 @@ class Verify : public ObjectWrap { return args.This(); } - static Handle - VerifyUpdate(const Arguments& args) { - Verify *verify = ObjectWrap::Unwrap(args.This()); + static Handle VerifyUpdate(const Arguments& args) { HandleScope scope; + Verify *verify = ObjectWrap::Unwrap(args.This()); + enum encoding enc = ParseEncoding(args[1]); ssize_t len = DecodeBytes(args[0], enc); @@ -2128,12 +2095,12 @@ class Verify : public ObjectWrap { return args.This(); } - static Handle - VerifyFinal(const Arguments& args) { - Verify *verify = ObjectWrap::Unwrap(args.This()); + static Handle VerifyFinal(const Arguments& args) { HandleScope scope; + Verify *verify = ObjectWrap::Unwrap(args.This()); + ssize_t klen = DecodeBytes(args[0], BINARY); if (klen < 0) { @@ -2152,7 +2119,7 @@ class Verify : public ObjectWrap { Local exception = Exception::TypeError(String::New("Bad argument")); return ThrowException(exception); } - + unsigned char* hbuf = new unsigned char[hlen]; ssize_t hwritten = DecodeWrite((char *)hbuf, hlen, args[1], BINARY); assert(hwritten == hlen); @@ -2168,39 +2135,36 @@ class Verify : public ObjectWrap { String::Utf8Value encoding(args[2]->ToString()); if (strcasecmp(*encoding, "hex") == 0) { // Hex encoding - hex_decode(hbuf, hlen, (char **)&dbuf, &dlen); + HexDecode(hbuf, hlen, (char **)&dbuf, &dlen); r = verify->VerifyFinal(kbuf, klen, dbuf, dlen); - free(dbuf); + free(dbuf); } else if (strcasecmp(*encoding, "base64") == 0) { // Base64 encoding unbase64(hbuf, hlen, (char **)&dbuf, &dlen); r = verify->VerifyFinal(kbuf, klen, dbuf, dlen); - free(dbuf); + free(dbuf); } else if (strcasecmp(*encoding, "binary") == 0) { r = verify->VerifyFinal(kbuf, klen, hbuf, hlen); } else { - fprintf(stderr, "node-crypto : Verify .verify encoding " - "can be binary, hex or base64\n"); + fprintf(stderr, "node-crypto : Verify .verify encoding " + "can be binary, hex or base64\n"); } } return scope.Close(Integer::New(r)); } - Verify () : ObjectWrap () - { - initialised = false; + Verify () : ObjectWrap () { + initialised_ = false; } - ~Verify () - { - } + ~Verify () { } private: EVP_MD_CTX mdctx; const EVP_MD *md; - bool initialised; + bool initialised_; }; @@ -2226,12 +2190,12 @@ void InitCrypto(Handle target) { Sign::Initialize(target); Verify::Initialize(target); - subject_symbol = NODE_PSYMBOL("subject"); - issuer_symbol = NODE_PSYMBOL("issuer"); - valid_from_symbol = NODE_PSYMBOL("valid_from"); - valid_to_symbol = NODE_PSYMBOL("valid_to"); - name_symbol = NODE_PSYMBOL("name"); - version_symbol = NODE_PSYMBOL("version"); + subject_symbol = NODE_PSYMBOL("subject"); + issuer_symbol = NODE_PSYMBOL("issuer"); + valid_from_symbol = NODE_PSYMBOL("valid_from"); + valid_to_symbol = NODE_PSYMBOL("valid_to"); + name_symbol = NODE_PSYMBOL("name"); + version_symbol = NODE_PSYMBOL("version"); }