Skip to content

Commit c7ebef7

Browse files
estliberitasFishrock123
authored andcommitted
crypto: simplify Certificate class bindings
Replace Certificate C++ class with simple functions. Update crypto.Certificate methods accordingly. PR-URL: #5382 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent aee1ff3 commit c7ebef7

File tree

3 files changed

+16
-66
lines changed

3 files changed

+16
-66
lines changed

lib/crypto.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -582,23 +582,21 @@ exports.Certificate = Certificate;
582582
function Certificate() {
583583
if (!(this instanceof Certificate))
584584
return new Certificate();
585-
586-
this._handle = new binding.Certificate();
587585
}
588586

589587

590588
Certificate.prototype.verifySpkac = function(object) {
591-
return this._handle.verifySpkac(object);
589+
return binding.certVerifySpkac(object);
592590
};
593591

594592

595593
Certificate.prototype.exportPublicKey = function(object, encoding) {
596-
return this._handle.exportPublicKey(toBuf(object, encoding));
594+
return binding.certExportPublicKey(toBuf(object, encoding));
597595
};
598596

599597

600598
Certificate.prototype.exportChallenge = function(object, encoding) {
601-
return this._handle.exportChallenge(toBuf(object, encoding));
599+
return binding.certExportChallenge(toBuf(object, encoding));
602600
};
603601

604602

src/node_crypto.cc

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5470,29 +5470,7 @@ void GetCurves(const FunctionCallbackInfo<Value>& args) {
54705470
}
54715471

54725472

5473-
void Certificate::Initialize(Environment* env, Local<Object> target) {
5474-
HandleScope scope(env->isolate());
5475-
5476-
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
5477-
5478-
t->InstanceTemplate()->SetInternalFieldCount(1);
5479-
5480-
env->SetProtoMethod(t, "verifySpkac", VerifySpkac);
5481-
env->SetProtoMethod(t, "exportPublicKey", ExportPublicKey);
5482-
env->SetProtoMethod(t, "exportChallenge", ExportChallenge);
5483-
5484-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Certificate"),
5485-
t->GetFunction());
5486-
}
5487-
5488-
5489-
void Certificate::New(const FunctionCallbackInfo<Value>& args) {
5490-
Environment* env = Environment::GetCurrent(args);
5491-
new Certificate(env, args.This());
5492-
}
5493-
5494-
5495-
bool Certificate::VerifySpkac(const char* data, unsigned int len) {
5473+
bool VerifySpkac(const char* data, unsigned int len) {
54965474
bool i = 0;
54975475
EVP_PKEY* pkey = nullptr;
54985476
NETSCAPE_SPKI* spki = nullptr;
@@ -5518,9 +5496,8 @@ bool Certificate::VerifySpkac(const char* data, unsigned int len) {
55185496
}
55195497

55205498

5521-
void Certificate::VerifySpkac(const FunctionCallbackInfo<Value>& args) {
5522-
Certificate* certificate = Unwrap<Certificate>(args.Holder());
5523-
Environment* env = certificate->env();
5499+
void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
5500+
Environment* env = Environment::GetCurrent(args);
55245501
bool i = false;
55255502

55265503
if (args.Length() < 1)
@@ -5535,13 +5512,13 @@ void Certificate::VerifySpkac(const FunctionCallbackInfo<Value>& args) {
55355512
char* data = Buffer::Data(args[0]);
55365513
CHECK_NE(data, nullptr);
55375514

5538-
i = certificate->VerifySpkac(data, length);
5515+
i = VerifySpkac(data, length);
55395516

55405517
args.GetReturnValue().Set(i);
55415518
}
55425519

55435520

5544-
const char* Certificate::ExportPublicKey(const char* data, int len) {
5521+
const char* ExportPublicKey(const char* data, int len) {
55455522
char* buf = nullptr;
55465523
EVP_PKEY* pkey = nullptr;
55475524
NETSCAPE_SPKI* spki = nullptr;
@@ -5582,11 +5559,9 @@ const char* Certificate::ExportPublicKey(const char* data, int len) {
55825559
}
55835560

55845561

5585-
void Certificate::ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
5562+
void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
55865563
Environment* env = Environment::GetCurrent(args);
55875564

5588-
Certificate* certificate = Unwrap<Certificate>(args.Holder());
5589-
55905565
if (args.Length() < 1)
55915566
return env->ThrowTypeError("Missing argument");
55925567

@@ -5599,7 +5574,7 @@ void Certificate::ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
55995574
char* data = Buffer::Data(args[0]);
56005575
CHECK_NE(data, nullptr);
56015576

5602-
const char* pkey = certificate->ExportPublicKey(data, length);
5577+
const char* pkey = ExportPublicKey(data, length);
56035578
if (pkey == nullptr)
56045579
return args.GetReturnValue().SetEmptyString();
56055580

@@ -5611,7 +5586,7 @@ void Certificate::ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
56115586
}
56125587

56135588

5614-
const char* Certificate::ExportChallenge(const char* data, int len) {
5589+
const char* ExportChallenge(const char* data, int len) {
56155590
NETSCAPE_SPKI* sp = nullptr;
56165591

56175592
sp = NETSCAPE_SPKI_b64_decode(data, len);
@@ -5627,11 +5602,9 @@ const char* Certificate::ExportChallenge(const char* data, int len) {
56275602
}
56285603

56295604

5630-
void Certificate::ExportChallenge(const FunctionCallbackInfo<Value>& args) {
5605+
void ExportChallenge(const FunctionCallbackInfo<Value>& args) {
56315606
Environment* env = Environment::GetCurrent(args);
56325607

5633-
Certificate* crt = Unwrap<Certificate>(args.Holder());
5634-
56355608
if (args.Length() < 1)
56365609
return env->ThrowTypeError("Missing argument");
56375610

@@ -5644,7 +5617,7 @@ void Certificate::ExportChallenge(const FunctionCallbackInfo<Value>& args) {
56445617
char* data = Buffer::Data(args[0]);
56455618
CHECK_NE(data, nullptr);
56465619

5647-
const char* cert = crt->ExportChallenge(data, len);
5620+
const char* cert = ExportChallenge(data, len);
56485621
if (cert == nullptr)
56495622
return args.GetReturnValue().SetEmptyString();
56505623

@@ -5753,8 +5726,10 @@ void InitCrypto(Local<Object> target,
57535726
Hash::Initialize(env, target);
57545727
Sign::Initialize(env, target);
57555728
Verify::Initialize(env, target);
5756-
Certificate::Initialize(env, target);
57575729

5730+
env->SetMethod(target, "certVerifySpkac", VerifySpkac);
5731+
env->SetMethod(target, "certExportPublicKey", ExportPublicKey);
5732+
env->SetMethod(target, "certExportChallenge", ExportChallenge);
57585733
#ifndef OPENSSL_NO_ENGINE
57595734
env->SetMethod(target, "setEngine", SetEngine);
57605735
#endif // !OPENSSL_NO_ENGINE

src/node_crypto.h

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -732,29 +732,6 @@ class ECDH : public BaseObject {
732732
const EC_GROUP* group_;
733733
};
734734

735-
class Certificate : public AsyncWrap {
736-
public:
737-
static void Initialize(Environment* env, v8::Local<v8::Object> target);
738-
739-
v8::Local<v8::Value> CertificateInit(const char* sign_type);
740-
bool VerifySpkac(const char* data, unsigned int len);
741-
const char* ExportPublicKey(const char* data, int len);
742-
const char* ExportChallenge(const char* data, int len);
743-
744-
size_t self_size() const override { return sizeof(*this); }
745-
746-
protected:
747-
static void New(const v8::FunctionCallbackInfo<v8::Value>& args);
748-
static void VerifySpkac(const v8::FunctionCallbackInfo<v8::Value>& args);
749-
static void ExportPublicKey(const v8::FunctionCallbackInfo<v8::Value>& args);
750-
static void ExportChallenge(const v8::FunctionCallbackInfo<v8::Value>& args);
751-
752-
Certificate(Environment* env, v8::Local<v8::Object> wrap)
753-
: AsyncWrap(env, wrap, AsyncWrap::PROVIDER_CRYPTO) {
754-
MakeWeak<Certificate>(this);
755-
}
756-
};
757-
758735
bool EntropySource(unsigned char* buffer, size_t length);
759736
#ifndef OPENSSL_NO_ENGINE
760737
void SetEngine(const v8::FunctionCallbackInfo<v8::Value>& args);

0 commit comments

Comments
 (0)