diff --git a/lib/crypto.js b/lib/crypto.js index 130d17d8a6f..f7f6437533f 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -2,7 +2,6 @@ try { var binding = process.binding('crypto'); var SecureContext = binding.SecureContext; - var SecureStream = binding.SecureStream; var Hmac = binding.Hmac; var Hash = binding.Hash; var Cipher = binding.Cipher; diff --git a/lib/tls.js b/lib/tls.js index 3598078233c..cb633847403 100644 --- a/lib/tls.js +++ b/lib/tls.js @@ -15,8 +15,14 @@ if (debugLevel & 0x2) { } -/* Lazy Loaded crypto object */ -var SecureStream = null; // note SecureStream is not a "real" stream. +var Connection = null; +try { + Connection = process.binding('crypto').Connection; +} +catch (e) { + throw new Error('node.js not compiled with openssl crypto support.'); +} + // Base class of both CleartextStream and EncryptedStream function CryptoStream (pair) { @@ -231,13 +237,6 @@ function SecurePair(credentials, isServer, requestCert, rejectUnauthorized) { var self = this; - try { - SecureStream = process.binding('crypto').SecureStream; - } - catch (e) { - throw new Error('node.js not compiled with openssl crypto support.'); - } - events.EventEmitter.call(this); this._secureEstablished = false; @@ -264,10 +263,10 @@ function SecurePair(credentials, isServer, requestCert, rejectUnauthorized) { this._rejectUnauthorized = rejectUnauthorized ? true : false; this._requestCert = requestCert ? true : false; - this._ssl = new SecureStream(this.credentials.context, - this._isServer ? true : false, - this._requestCert, - this._rejectUnauthorized); + this._ssl = new Connection(this.credentials.context, + this._isServer ? true : false, + this._requestCert, + this._rejectUnauthorized); /* Acts as a r/w stream to the cleartext side of the stream. */ diff --git a/src/node_crypto.cc b/src/node_crypto.cc index db1fdcc2303..3970a2a514f 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -17,6 +17,7 @@ #endif namespace node { +namespace crypto { using namespace v8; @@ -307,29 +308,29 @@ static int serr(SSL *ssl, const char* func, int rv) { return 0; } -void SecureStream::Initialize(Handle target) { +void Connection::Initialize(Handle target) { HandleScope scope; - Local t = FunctionTemplate::New(SecureStream::New); + Local t = FunctionTemplate::New(Connection::New); t->InstanceTemplate()->SetInternalFieldCount(1); - t->SetClassName(String::NewSymbol("SecureStream")); + t->SetClassName(String::NewSymbol("Connection")); - NODE_SET_PROTOTYPE_METHOD(t, "encIn", SecureStream::EncIn); - NODE_SET_PROTOTYPE_METHOD(t, "clearOut", SecureStream::ClearOut); - NODE_SET_PROTOTYPE_METHOD(t, "clearIn", SecureStream::ClearIn); - NODE_SET_PROTOTYPE_METHOD(t, "encOut", SecureStream::EncOut); - NODE_SET_PROTOTYPE_METHOD(t, "clearPending", SecureStream::ClearPending); - NODE_SET_PROTOTYPE_METHOD(t, "encPending", SecureStream::EncPending); - NODE_SET_PROTOTYPE_METHOD(t, "getPeerCertificate", SecureStream::GetPeerCertificate); - NODE_SET_PROTOTYPE_METHOD(t, "isInitFinished", SecureStream::IsInitFinished); - NODE_SET_PROTOTYPE_METHOD(t, "verifyError", SecureStream::VerifyError); - NODE_SET_PROTOTYPE_METHOD(t, "getCurrentCipher", SecureStream::GetCurrentCipher); - NODE_SET_PROTOTYPE_METHOD(t, "start", SecureStream::Start); - NODE_SET_PROTOTYPE_METHOD(t, "shutdown", SecureStream::Shutdown); - NODE_SET_PROTOTYPE_METHOD(t, "receivedShutdown", SecureStream::ReceivedShutdown); - NODE_SET_PROTOTYPE_METHOD(t, "close", SecureStream::Close); + NODE_SET_PROTOTYPE_METHOD(t, "encIn", Connection::EncIn); + NODE_SET_PROTOTYPE_METHOD(t, "clearOut", Connection::ClearOut); + NODE_SET_PROTOTYPE_METHOD(t, "clearIn", Connection::ClearIn); + NODE_SET_PROTOTYPE_METHOD(t, "encOut", Connection::EncOut); + NODE_SET_PROTOTYPE_METHOD(t, "clearPending", Connection::ClearPending); + NODE_SET_PROTOTYPE_METHOD(t, "encPending", Connection::EncPending); + NODE_SET_PROTOTYPE_METHOD(t, "getPeerCertificate", Connection::GetPeerCertificate); + NODE_SET_PROTOTYPE_METHOD(t, "isInitFinished", Connection::IsInitFinished); + NODE_SET_PROTOTYPE_METHOD(t, "verifyError", Connection::VerifyError); + NODE_SET_PROTOTYPE_METHOD(t, "getCurrentCipher", Connection::GetCurrentCipher); + NODE_SET_PROTOTYPE_METHOD(t, "start", Connection::Start); + NODE_SET_PROTOTYPE_METHOD(t, "shutdown", Connection::Shutdown); + NODE_SET_PROTOTYPE_METHOD(t, "receivedShutdown", Connection::ReceivedShutdown); + NODE_SET_PROTOTYPE_METHOD(t, "close", Connection::Close); - target->Set(String::NewSymbol("SecureStream"), t->GetFunction()); + target->Set(String::NewSymbol("Connection"), t->GetFunction()); } @@ -373,16 +374,16 @@ static int VerifyCallback(int preverify_ok, X509_STORE_CTX *ctx) { // // Since we cannot perform I/O quickly enough in this callback, we ignore // all preverify_ok errors and let the handshake continue. It is - // imparative that the user use SecureStream::VerifyError after the + // imparative that the user use Connection::VerifyError after the // 'secure' callback has been made. return 1; } -Handle SecureStream::New(const Arguments& args) { +Handle Connection::New(const Arguments& args) { HandleScope scope; - SecureStream *p = new SecureStream(); + Connection *p = new Connection(); p->Wrap(args.Holder()); if (args.Length() < 1 || !args[0]->IsObject()) { @@ -435,10 +436,10 @@ Handle SecureStream::New(const Arguments& args) { } -Handle SecureStream::EncIn(const Arguments& args) { +Handle Connection::EncIn(const Arguments& args) { HandleScope scope; - SecureStream *ss = ObjectWrap::Unwrap(args.Holder()); + Connection *ss = ObjectWrap::Unwrap(args.Holder()); if (args.Length() < 3) { return ThrowException(Exception::TypeError( @@ -477,10 +478,10 @@ Handle SecureStream::EncIn(const Arguments& args) { } -Handle SecureStream::ClearOut(const Arguments& args) { +Handle Connection::ClearOut(const Arguments& args) { HandleScope scope; - SecureStream *ss = ObjectWrap::Unwrap(args.Holder()); + Connection *ss = ObjectWrap::Unwrap(args.Holder()); if (args.Length() < 3) { return ThrowException(Exception::TypeError( @@ -531,28 +532,28 @@ Handle SecureStream::ClearOut(const Arguments& args) { } -Handle SecureStream::ClearPending(const Arguments& args) { +Handle Connection::ClearPending(const Arguments& args) { HandleScope scope; - SecureStream *ss = ObjectWrap::Unwrap(args.Holder()); + Connection *ss = ObjectWrap::Unwrap(args.Holder()); int bytes_pending = BIO_pending(ss->bio_read_); return scope.Close(Integer::New(bytes_pending)); } -Handle SecureStream::EncPending(const Arguments& args) { +Handle Connection::EncPending(const Arguments& args) { HandleScope scope; - SecureStream *ss = ObjectWrap::Unwrap(args.Holder()); + Connection *ss = ObjectWrap::Unwrap(args.Holder()); int bytes_pending = BIO_pending(ss->bio_write_); return scope.Close(Integer::New(bytes_pending)); } -Handle SecureStream::EncOut(const Arguments& args) { +Handle Connection::EncOut(const Arguments& args) { HandleScope scope; - SecureStream *ss = ObjectWrap::Unwrap(args.Holder()); + Connection *ss = ObjectWrap::Unwrap(args.Holder()); if (args.Length() < 3) { return ThrowException(Exception::TypeError( @@ -586,10 +587,10 @@ Handle SecureStream::EncOut(const Arguments& args) { } -Handle SecureStream::ClearIn(const Arguments& args) { +Handle Connection::ClearIn(const Arguments& args) { HandleScope scope; - SecureStream *ss = ObjectWrap::Unwrap(args.Holder()); + Connection *ss = ObjectWrap::Unwrap(args.Holder()); if (args.Length() < 3) { return ThrowException(Exception::TypeError( @@ -642,10 +643,10 @@ Handle SecureStream::ClearIn(const Arguments& args) { } -Handle SecureStream::GetPeerCertificate(const Arguments& args) { +Handle Connection::GetPeerCertificate(const Arguments& args) { HandleScope scope; - SecureStream *ss = ObjectWrap::Unwrap(args.Holder()); + Connection *ss = ObjectWrap::Unwrap(args.Holder()); if (ss->ssl_ == NULL) return Undefined(); Local info = Object::New(); @@ -700,11 +701,11 @@ Handle SecureStream::GetPeerCertificate(const Arguments& args) { return scope.Close(info); } -Handle SecureStream::Start(const Arguments& args) { +Handle Connection::Start(const Arguments& args) { HandleScope scope; int rv; - SecureStream *ss = ObjectWrap::Unwrap(args.Holder()); + Connection *ss = ObjectWrap::Unwrap(args.Holder()); if (!SSL_is_init_finished(ss->ssl_)) { if (ss->is_server_) { @@ -728,10 +729,10 @@ Handle SecureStream::Start(const Arguments& args) { } -Handle SecureStream::Shutdown(const Arguments& args) { +Handle Connection::Shutdown(const Arguments& args) { HandleScope scope; - SecureStream *ss = ObjectWrap::Unwrap(args.Holder()); + Connection *ss = ObjectWrap::Unwrap(args.Holder()); if (ss->ssl_ == NULL) return False(); int r = SSL_shutdown(ss->ssl_); @@ -740,10 +741,10 @@ Handle SecureStream::Shutdown(const Arguments& args) { } -Handle SecureStream::ReceivedShutdown(const Arguments& args) { +Handle Connection::ReceivedShutdown(const Arguments& args) { HandleScope scope; - SecureStream *ss = ObjectWrap::Unwrap(args.Holder()); + Connection *ss = ObjectWrap::Unwrap(args.Holder()); if (ss->ssl_ == NULL) return False(); int r = SSL_get_shutdown(ss->ssl_); @@ -754,18 +755,18 @@ Handle SecureStream::ReceivedShutdown(const Arguments& args) { } -Handle SecureStream::IsInitFinished(const Arguments& args) { +Handle Connection::IsInitFinished(const Arguments& args) { HandleScope scope; - SecureStream *ss = ObjectWrap::Unwrap(args.Holder()); + Connection *ss = ObjectWrap::Unwrap(args.Holder()); if (ss->ssl_ == NULL) return False(); return SSL_is_init_finished(ss->ssl_) ? True() : False(); } -Handle SecureStream::VerifyError(const Arguments& args) { +Handle Connection::VerifyError(const Arguments& args) { HandleScope scope; - SecureStream *ss = ObjectWrap::Unwrap(args.Holder()); + Connection *ss = ObjectWrap::Unwrap(args.Holder()); if (ss->ssl_ == NULL) return Null(); @@ -906,10 +907,10 @@ Handle SecureStream::VerifyError(const Arguments& args) { } -Handle SecureStream::GetCurrentCipher(const Arguments& args) { +Handle Connection::GetCurrentCipher(const Arguments& args) { HandleScope scope; - SecureStream *ss = ObjectWrap::Unwrap(args.Holder()); + Connection *ss = ObjectWrap::Unwrap(args.Holder()); OPENSSL_CONST SSL_CIPHER *c; if ( ss->ssl_ == NULL ) return Undefined(); @@ -923,10 +924,10 @@ Handle SecureStream::GetCurrentCipher(const Arguments& args) { return scope.Close(info); } -Handle SecureStream::Close(const Arguments& args) { +Handle Connection::Close(const Arguments& args) { HandleScope scope; - SecureStream *ss = ObjectWrap::Unwrap(args.Holder()); + Connection *ss = ObjectWrap::Unwrap(args.Holder()); if (ss->ssl_ != NULL) { SSL_free(ss->ssl_); @@ -2667,7 +2668,7 @@ void InitCrypto(Handle target) { ERR_load_crypto_strings(); SecureContext::Initialize(target); - SecureStream::Initialize(target); + Connection::Initialize(target); Cipher::Initialize(target); Decipher::Initialize(target); Hmac::Initialize(target); @@ -2684,7 +2685,8 @@ void InitCrypto(Handle target) { version_symbol = NODE_PSYMBOL("version"); } +} // namespace crypto } // namespace node -NODE_MODULE(node_crypto, node::InitCrypto); +NODE_MODULE(node_crypto, node::crypto::InitCrypto); diff --git a/src/node_crypto.h b/src/node_crypto.h index 52404b210b2..73120aa8600 100644 --- a/src/node_crypto.h +++ b/src/node_crypto.h @@ -16,6 +16,7 @@ namespace node { +namespace crypto { class SecureContext : ObjectWrap { public: @@ -52,7 +53,7 @@ class SecureContext : ObjectWrap { private: }; -class SecureStream : ObjectWrap { +class Connection : ObjectWrap { public: static void Initialize(v8::Handle target); @@ -73,12 +74,12 @@ class SecureStream : ObjectWrap { static v8::Handle Start(const v8::Arguments& args); static v8::Handle Close(const v8::Arguments& args); - SecureStream() : ObjectWrap() { + Connection() : ObjectWrap() { bio_read_ = bio_write_ = NULL; ssl_ = NULL; } - ~SecureStream() { + ~Connection() { if (ssl_ != NULL) { SSL_free(ssl_); ssl_ = NULL; @@ -93,6 +94,8 @@ class SecureStream : ObjectWrap { }; void InitCrypto(v8::Handle target); -} + +} // namespace crypto +} // namespace node #endif // SRC_NODE_CRYPTO_H_