Skip to content

Commit a37401e

Browse files
estliberitasbnoordhuis
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 c490b8b commit a37401e

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
@@ -591,23 +591,21 @@ exports.Certificate = Certificate;
591591
function Certificate() {
592592
if (!(this instanceof Certificate))
593593
return new Certificate();
594-
595-
this._handle = new binding.Certificate();
596594
}
597595

598596

599597
Certificate.prototype.verifySpkac = function(object) {
600-
return this._handle.verifySpkac(object);
598+
return binding.certVerifySpkac(object);
601599
};
602600

603601

604602
Certificate.prototype.exportPublicKey = function(object, encoding) {
605-
return this._handle.exportPublicKey(toBuf(object, encoding));
603+
return binding.certExportPublicKey(toBuf(object, encoding));
606604
};
607605

608606

609607
Certificate.prototype.exportChallenge = function(object, encoding) {
610-
return this._handle.exportChallenge(toBuf(object, encoding));
608+
return binding.certExportChallenge(toBuf(object, encoding));
611609
};
612610

613611

src/node_crypto.cc

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

54865486

5487-
void Certificate::Initialize(Environment* env, Local<Object> target) {
5488-
HandleScope scope(env->isolate());
5489-
5490-
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
5491-
5492-
t->InstanceTemplate()->SetInternalFieldCount(1);
5493-
5494-
env->SetProtoMethod(t, "verifySpkac", VerifySpkac);
5495-
env->SetProtoMethod(t, "exportPublicKey", ExportPublicKey);
5496-
env->SetProtoMethod(t, "exportChallenge", ExportChallenge);
5497-
5498-
target->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "Certificate"),
5499-
t->GetFunction());
5500-
}
5501-
5502-
5503-
void Certificate::New(const FunctionCallbackInfo<Value>& args) {
5504-
Environment* env = Environment::GetCurrent(args);
5505-
new Certificate(env, args.This());
5506-
}
5507-
5508-
5509-
bool Certificate::VerifySpkac(const char* data, unsigned int len) {
5487+
bool VerifySpkac(const char* data, unsigned int len) {
55105488
bool i = 0;
55115489
EVP_PKEY* pkey = nullptr;
55125490
NETSCAPE_SPKI* spki = nullptr;
@@ -5532,9 +5510,8 @@ bool Certificate::VerifySpkac(const char* data, unsigned int len) {
55325510
}
55335511

55345512

5535-
void Certificate::VerifySpkac(const FunctionCallbackInfo<Value>& args) {
5536-
Certificate* certificate = Unwrap<Certificate>(args.Holder());
5537-
Environment* env = certificate->env();
5513+
void VerifySpkac(const FunctionCallbackInfo<Value>& args) {
5514+
Environment* env = Environment::GetCurrent(args);
55385515
bool i = false;
55395516

55405517
if (args.Length() < 1)
@@ -5549,13 +5526,13 @@ void Certificate::VerifySpkac(const FunctionCallbackInfo<Value>& args) {
55495526
char* data = Buffer::Data(args[0]);
55505527
CHECK_NE(data, nullptr);
55515528

5552-
i = certificate->VerifySpkac(data, length);
5529+
i = VerifySpkac(data, length);
55535530

55545531
args.GetReturnValue().Set(i);
55555532
}
55565533

55575534

5558-
const char* Certificate::ExportPublicKey(const char* data, int len) {
5535+
const char* ExportPublicKey(const char* data, int len) {
55595536
char* buf = nullptr;
55605537
EVP_PKEY* pkey = nullptr;
55615538
NETSCAPE_SPKI* spki = nullptr;
@@ -5596,11 +5573,9 @@ const char* Certificate::ExportPublicKey(const char* data, int len) {
55965573
}
55975574

55985575

5599-
void Certificate::ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
5576+
void ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
56005577
Environment* env = Environment::GetCurrent(args);
56015578

5602-
Certificate* certificate = Unwrap<Certificate>(args.Holder());
5603-
56045579
if (args.Length() < 1)
56055580
return env->ThrowTypeError("Missing argument");
56065581

@@ -5613,7 +5588,7 @@ void Certificate::ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
56135588
char* data = Buffer::Data(args[0]);
56145589
CHECK_NE(data, nullptr);
56155590

5616-
const char* pkey = certificate->ExportPublicKey(data, length);
5591+
const char* pkey = ExportPublicKey(data, length);
56175592
if (pkey == nullptr)
56185593
return args.GetReturnValue().SetEmptyString();
56195594

@@ -5625,7 +5600,7 @@ void Certificate::ExportPublicKey(const FunctionCallbackInfo<Value>& args) {
56255600
}
56265601

56275602

5628-
const char* Certificate::ExportChallenge(const char* data, int len) {
5603+
const char* ExportChallenge(const char* data, int len) {
56295604
NETSCAPE_SPKI* sp = nullptr;
56305605

56315606
sp = NETSCAPE_SPKI_b64_decode(data, len);
@@ -5641,11 +5616,9 @@ const char* Certificate::ExportChallenge(const char* data, int len) {
56415616
}
56425617

56435618

5644-
void Certificate::ExportChallenge(const FunctionCallbackInfo<Value>& args) {
5619+
void ExportChallenge(const FunctionCallbackInfo<Value>& args) {
56455620
Environment* env = Environment::GetCurrent(args);
56465621

5647-
Certificate* crt = Unwrap<Certificate>(args.Holder());
5648-
56495622
if (args.Length() < 1)
56505623
return env->ThrowTypeError("Missing argument");
56515624

@@ -5658,7 +5631,7 @@ void Certificate::ExportChallenge(const FunctionCallbackInfo<Value>& args) {
56585631
char* data = Buffer::Data(args[0]);
56595632
CHECK_NE(data, nullptr);
56605633

5661-
const char* cert = crt->ExportChallenge(data, len);
5634+
const char* cert = ExportChallenge(data, len);
56625635
if (cert == nullptr)
56635636
return args.GetReturnValue().SetEmptyString();
56645637

@@ -5797,8 +5770,10 @@ void InitCrypto(Local<Object> target,
57975770
Hash::Initialize(env, target);
57985771
Sign::Initialize(env, target);
57995772
Verify::Initialize(env, target);
5800-
Certificate::Initialize(env, target);
58015773

5774+
env->SetMethod(target, "certVerifySpkac", VerifySpkac);
5775+
env->SetMethod(target, "certExportPublicKey", ExportPublicKey);
5776+
env->SetMethod(target, "certExportChallenge", ExportChallenge);
58025777
#ifndef OPENSSL_NO_ENGINE
58035778
env->SetMethod(target, "setEngine", SetEngine);
58045779
#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)