Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@
},
"license": "MIT",
"dependencies": {
"nan": "2.12.0"
"nan": "2.13.2"
}
}
58 changes: 43 additions & 15 deletions src/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,54 @@
using namespace v8;

void init(Local<Object> exports) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::HandleScope handle_scope(isolate);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nan has a helper for these purposes Nan::GetCurrentContext

Nan::Set(exports,
Nan::New<String>("version").ToLocalChecked(),
Nan::New<String>(VERSION).ToLocalChecked());

Nan::Set(exports,
Nan::New<String>("verify").ToLocalChecked(),
Nan::New<FunctionTemplate>(verify)->GetFunction());
{
v8::Local<v8::Function> method;
Nan::New<FunctionTemplate>(verify)->GetFunction(context).ToLocal<v8::Function>(&method);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. ToLocal does not check if monad is empty or not. Better to use ToLocalChecked()
  2. It is a little bit stinky, to use lexical scopes for each set.

Nan::Set(exports,
Nan::New<String>("verify").ToLocalChecked(),
method
);
}

Nan::Set(exports,
Nan::New<String>("getAltNames").ToLocalChecked(),
Nan::New<FunctionTemplate>(get_altnames)->GetFunction());
Nan::Set(exports,
Nan::New<String>("getSubject").ToLocalChecked(),
Nan::New<FunctionTemplate>(get_subject)->GetFunction());
Nan::Set(exports,
Nan::New<String>("getIssuer").ToLocalChecked(),
Nan::New<FunctionTemplate>(get_issuer)->GetFunction());
Nan::Set(exports,
Nan::New<String>("parseCert").ToLocalChecked(),
Nan::New<FunctionTemplate>(parse_cert)->GetFunction());
{
v8::Local<v8::Function> method;
Nan::New<FunctionTemplate>(get_altnames)->GetFunction(context).ToLocal<v8::Function>(&method);
Nan::Set(exports,
Nan::New<String>("getAltNames").ToLocalChecked(),
method
);
}
{
v8::Local<v8::Function> method;
Nan::New<FunctionTemplate>(get_subject)->GetFunction(context).ToLocal<v8::Function>(&method);
Nan::Set(exports,
Nan::New<String>("getSubject").ToLocalChecked(),
method
);
}
{
v8::Local<v8::Function> method;
Nan::New<FunctionTemplate>(get_issuer)->GetFunction(context).ToLocal<v8::Function>(&method);
Nan::Set(exports,
Nan::New<String>("getIssuer").ToLocalChecked(),
method
);
}
{
v8::Local<v8::Function> method;
Nan::New<FunctionTemplate>(parse_cert)->GetFunction(context).ToLocal<v8::Function>(&method);
Nan::Set(exports,
Nan::New<String>("parseCert").ToLocalChecked(),
method
);
}
}

NODE_MODULE(x509, init)
25 changes: 16 additions & 9 deletions src/x509.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ static const char *MISSING[4][2] = {
};

std::string parse_args(const Nan::FunctionCallbackInfo<v8::Value>& info) {
v8::Isolate* isolate = v8::Isolate::GetCurrent();
if (info.Length() == 0) {
Nan::ThrowTypeError("Must provide a certificate string.");
return std::string();
Expand All @@ -38,22 +39,23 @@ std::string parse_args(const Nan::FunctionCallbackInfo<v8::Value>& info) {
return std::string();
}

if (info[0]->ToString()->Length() == 0) {
if (info[0]->ToString(isolate)->Length() == 0) {
Nan::ThrowTypeError("Certificate argument provided, but left blank.");
return std::string();
}

return *Nan::Utf8String(info[0]->ToString());
return *Nan::Utf8String(info[0]->ToString(isolate));
}



NAN_METHOD(verify) {
Nan::HandleScope scope;
OpenSSL_add_all_algorithms();
v8::Isolate* isolate = v8::Isolate::GetCurrent();

std::string cert_path = *String::Utf8Value(info[0]->ToString());
std::string ca_bundlestr = *String::Utf8Value(info[1]->ToString());
std::string cert_path = *String::Utf8Value(isolate, info[0]->ToString(isolate));
std::string ca_bundlestr = *String::Utf8Value(isolate, info[1]->ToString(isolate));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can get V8 Isolate by Nan::GetCurrentContext()->GetIsolate()


X509_STORE *store = NULL;
X509_STORE_CTX *verify_ctx = NULL;
Expand Down Expand Up @@ -112,10 +114,11 @@ NAN_METHOD(verify) {
NAN_METHOD(get_altnames) {
Nan::HandleScope scope;
std::string parsed_arg = parse_args(info);
v8::Isolate* isolate = v8::Isolate::GetCurrent();
if(parsed_arg.size() == 0) {
info.GetReturnValue().SetUndefined();
}
Local<Object> exports(try_parse(parsed_arg)->ToObject());
Local<Object> exports(try_parse(parsed_arg)->ToObject(isolate));
Local<Value> key = Nan::New<String>("altNames").ToLocalChecked();
info.GetReturnValue().Set(
Nan::Get(exports, key).ToLocalChecked());
Expand All @@ -125,10 +128,11 @@ NAN_METHOD(get_altnames) {
NAN_METHOD(get_subject) {
Nan::HandleScope scope;
std::string parsed_arg = parse_args(info);
v8::Isolate* isolate = v8::Isolate::GetCurrent();
if(parsed_arg.size() == 0) {
info.GetReturnValue().SetUndefined();
}
Local<Object> exports(try_parse(parsed_arg)->ToObject());
Local<Object> exports(try_parse(parsed_arg)->ToObject(isolate));
Local<Value> key = Nan::New<String>("subject").ToLocalChecked();
info.GetReturnValue().Set(
Nan::Get(exports, key).ToLocalChecked());
Expand All @@ -137,11 +141,12 @@ NAN_METHOD(get_subject) {

NAN_METHOD(get_issuer) {
Nan::HandleScope scope;
v8::Isolate* isolate = v8::Isolate::GetCurrent();
std::string parsed_arg = parse_args(info);
if(parsed_arg.size() == 0) {
info.GetReturnValue().SetUndefined();
}
Local<Object> exports(try_parse(parsed_arg)->ToObject());
Local<Object> exports(try_parse(parsed_arg)->ToObject(isolate));
Local<Value> key = Nan::New<String>("issuer").ToLocalChecked();
info.GetReturnValue().Set(
Nan::Get(exports, key).ToLocalChecked());
Expand All @@ -150,11 +155,12 @@ NAN_METHOD(get_issuer) {

NAN_METHOD(parse_cert) {
Nan::HandleScope scope;
v8::Isolate* isolate = v8::Isolate::GetCurrent();
std::string parsed_arg = parse_args(info);
if(parsed_arg.size() == 0) {
info.GetReturnValue().SetUndefined();
}
Local<Object> exports(try_parse(parsed_arg)->ToObject());
Local<Object> exports(try_parse(parsed_arg)->ToObject(isolate));
info.GetReturnValue().Set(exports);
ERR_clear_error();
}
Expand Down Expand Up @@ -444,6 +450,7 @@ Local<Value> parse_serial(ASN1_INTEGER *serial) {

Local<Value> parse_date(ASN1_TIME *date) {
Nan::EscapableHandleScope scope;
v8::Isolate* isolate = v8::Isolate::GetCurrent();
BIO *bio;
BUF_MEM *bm;
char formatted[64];
Expand All @@ -459,7 +466,7 @@ Local<Value> parse_date(ASN1_TIME *date) {

Local<Object> global = Nan::GetCurrentContext()->Global();
Local<Object> DateObject = Nan::Get(global,
Nan::New<String>("Date").ToLocalChecked()).ToLocalChecked()->ToObject();
Nan::New<String>("Date").ToLocalChecked()).ToLocalChecked()->ToObject(isolate);
return scope.Escape(Nan::CallAsConstructor(DateObject, 1, args).ToLocalChecked());
}

Expand Down