From c7ebef709ffde812cf7ec0baa5022e5f1011f87c Mon Sep 17 00:00:00 2001 From: Alexander Makarenko Date: Tue, 23 Feb 2016 15:43:32 +0300 Subject: [PATCH] crypto: simplify Certificate class bindings Replace Certificate C++ class with simple functions. Update crypto.Certificate methods accordingly. PR-URL: https://github.com/nodejs/node/pull/5382 Reviewed-By: Ben Noordhuis --- lib/crypto.js | 8 +++----- src/node_crypto.cc | 51 ++++++++++++---------------------------------- src/node_crypto.h | 23 --------------------- 3 files changed, 16 insertions(+), 66 deletions(-) diff --git a/lib/crypto.js b/lib/crypto.js index b32d9aff90b72b..d3c4b9e89d4530 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -582,23 +582,21 @@ exports.Certificate = Certificate; function Certificate() { if (!(this instanceof Certificate)) return new Certificate(); - - this._handle = new binding.Certificate(); } Certificate.prototype.verifySpkac = function(object) { - return this._handle.verifySpkac(object); + return binding.certVerifySpkac(object); }; Certificate.prototype.exportPublicKey = function(object, encoding) { - return this._handle.exportPublicKey(toBuf(object, encoding)); + return binding.certExportPublicKey(toBuf(object, encoding)); }; Certificate.prototype.exportChallenge = function(object, encoding) { - return this._handle.exportChallenge(toBuf(object, encoding)); + return binding.certExportChallenge(toBuf(object, encoding)); }; diff --git a/src/node_crypto.cc b/src/node_crypto.cc index acd83e9f2f41ad..a43633a2a3ce42 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -5470,29 +5470,7 @@ void GetCurves(const FunctionCallbackInfo& args) { } -void Certificate::Initialize(Environment* env, Local target) { - HandleScope scope(env->isolate()); - - Local t = env->NewFunctionTemplate(New); - - t->InstanceTemplate()->SetInternalFieldCount(1); - - env->SetProtoMethod(t, "verifySpkac", VerifySpkac); - env->SetProtoMethod(t, "exportPublicKey", ExportPublicKey); - env->SetProtoMethod(t, "exportChallenge", ExportChallenge); - - target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Certificate"), - t->GetFunction()); -} - - -void Certificate::New(const FunctionCallbackInfo& args) { - Environment* env = Environment::GetCurrent(args); - new Certificate(env, args.This()); -} - - -bool Certificate::VerifySpkac(const char* data, unsigned int len) { +bool VerifySpkac(const char* data, unsigned int len) { bool i = 0; EVP_PKEY* pkey = nullptr; NETSCAPE_SPKI* spki = nullptr; @@ -5518,9 +5496,8 @@ bool Certificate::VerifySpkac(const char* data, unsigned int len) { } -void Certificate::VerifySpkac(const FunctionCallbackInfo& args) { - Certificate* certificate = Unwrap(args.Holder()); - Environment* env = certificate->env(); +void VerifySpkac(const FunctionCallbackInfo& args) { + Environment* env = Environment::GetCurrent(args); bool i = false; if (args.Length() < 1) @@ -5535,13 +5512,13 @@ void Certificate::VerifySpkac(const FunctionCallbackInfo& args) { char* data = Buffer::Data(args[0]); CHECK_NE(data, nullptr); - i = certificate->VerifySpkac(data, length); + i = VerifySpkac(data, length); args.GetReturnValue().Set(i); } -const char* Certificate::ExportPublicKey(const char* data, int len) { +const char* ExportPublicKey(const char* data, int len) { char* buf = nullptr; EVP_PKEY* pkey = nullptr; NETSCAPE_SPKI* spki = nullptr; @@ -5582,11 +5559,9 @@ const char* Certificate::ExportPublicKey(const char* data, int len) { } -void Certificate::ExportPublicKey(const FunctionCallbackInfo& args) { +void ExportPublicKey(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - Certificate* certificate = Unwrap(args.Holder()); - if (args.Length() < 1) return env->ThrowTypeError("Missing argument"); @@ -5599,7 +5574,7 @@ void Certificate::ExportPublicKey(const FunctionCallbackInfo& args) { char* data = Buffer::Data(args[0]); CHECK_NE(data, nullptr); - const char* pkey = certificate->ExportPublicKey(data, length); + const char* pkey = ExportPublicKey(data, length); if (pkey == nullptr) return args.GetReturnValue().SetEmptyString(); @@ -5611,7 +5586,7 @@ void Certificate::ExportPublicKey(const FunctionCallbackInfo& args) { } -const char* Certificate::ExportChallenge(const char* data, int len) { +const char* ExportChallenge(const char* data, int len) { NETSCAPE_SPKI* sp = nullptr; sp = NETSCAPE_SPKI_b64_decode(data, len); @@ -5627,11 +5602,9 @@ const char* Certificate::ExportChallenge(const char* data, int len) { } -void Certificate::ExportChallenge(const FunctionCallbackInfo& args) { +void ExportChallenge(const FunctionCallbackInfo& args) { Environment* env = Environment::GetCurrent(args); - Certificate* crt = Unwrap(args.Holder()); - if (args.Length() < 1) return env->ThrowTypeError("Missing argument"); @@ -5644,7 +5617,7 @@ void Certificate::ExportChallenge(const FunctionCallbackInfo& args) { char* data = Buffer::Data(args[0]); CHECK_NE(data, nullptr); - const char* cert = crt->ExportChallenge(data, len); + const char* cert = ExportChallenge(data, len); if (cert == nullptr) return args.GetReturnValue().SetEmptyString(); @@ -5753,8 +5726,10 @@ void InitCrypto(Local target, Hash::Initialize(env, target); Sign::Initialize(env, target); Verify::Initialize(env, target); - Certificate::Initialize(env, target); + env->SetMethod(target, "certVerifySpkac", VerifySpkac); + env->SetMethod(target, "certExportPublicKey", ExportPublicKey); + env->SetMethod(target, "certExportChallenge", ExportChallenge); #ifndef OPENSSL_NO_ENGINE env->SetMethod(target, "setEngine", SetEngine); #endif // !OPENSSL_NO_ENGINE diff --git a/src/node_crypto.h b/src/node_crypto.h index 8a6c66703720b0..66c50efa2c040d 100644 --- a/src/node_crypto.h +++ b/src/node_crypto.h @@ -732,29 +732,6 @@ class ECDH : public BaseObject { const EC_GROUP* group_; }; -class Certificate : public AsyncWrap { - public: - static void Initialize(Environment* env, v8::Local target); - - v8::Local CertificateInit(const char* sign_type); - bool VerifySpkac(const char* data, unsigned int len); - const char* ExportPublicKey(const char* data, int len); - const char* ExportChallenge(const char* data, int len); - - size_t self_size() const override { return sizeof(*this); } - - protected: - static void New(const v8::FunctionCallbackInfo& args); - static void VerifySpkac(const v8::FunctionCallbackInfo& args); - static void ExportPublicKey(const v8::FunctionCallbackInfo& args); - static void ExportChallenge(const v8::FunctionCallbackInfo& args); - - Certificate(Environment* env, v8::Local wrap) - : AsyncWrap(env, wrap, AsyncWrap::PROVIDER_CRYPTO) { - MakeWeak(this); - } -}; - bool EntropySource(unsigned char* buffer, size_t length); #ifndef OPENSSL_NO_ENGINE void SetEngine(const v8::FunctionCallbackInfo& args);