Skip to content

Commit 6033d4b

Browse files
mbwhiteyorkie
authored andcommitted
Updated version #71 with formatting corrected (#74)
This is the #71 but with corrected formatting as per the discussion in #71 Thank you to those who worked on fix; aim here is to get the fix merged as it is needed by many projects Signed-off-by: Matthew B. White <[email protected]>
1 parent d7e6c43 commit 6033d4b

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@
1313
},
1414
"license": "MIT",
1515
"dependencies": {
16-
"nan": "2.2.0"
16+
"nan": "2.12.0"
1717
}
1818
}

src/x509.cc

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ std::string parse_args(const Nan::FunctionCallbackInfo<v8::Value>& info) {
4343
return std::string();
4444
}
4545

46-
return *String::Utf8Value(info[0]->ToString());
46+
return *Nan::Utf8String(info[0]->ToString());
4747
}
4848

4949

@@ -91,7 +91,7 @@ NAN_METHOD(verify) {
9191
X509_STORE_CTX_init(verify_ctx, store, cert, NULL);
9292
ret = X509_verify_cert(verify_ctx);
9393
if (ret <= 0) {
94-
error = X509_verify_cert_error_string(verify_ctx->error);
94+
error = X509_verify_cert_error_string(X509_STORE_CTX_get_error(verify_ctx));
9595
break;
9696
}
9797
} while(0);
@@ -237,7 +237,11 @@ Local<Value> try_parse(const std::string& dataString) {
237237
Nan::New<String>(stream.str()).ToLocalChecked());
238238

239239
// Signature Algorithm
240+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
241+
int sig_alg_nid = X509_get_signature_nid(cert);
242+
#else
240243
int sig_alg_nid = OBJ_obj2nid(cert->sig_alg->algorithm);
244+
#endif
241245
if (sig_alg_nid == NID_undef) {
242246
ERR_clear_error();
243247
Nan::ThrowError("unable to find specified signature algorithm name.");
@@ -272,7 +276,11 @@ Local<Value> try_parse(const std::string& dataString) {
272276
}
273277

274278
// public key
279+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
280+
int pkey_nid = X509_get_signature_nid(cert);
281+
#else
275282
int pkey_nid = OBJ_obj2nid(cert->cert_info->key->algor->algorithm);
283+
#endif
276284
if (pkey_nid == NID_undef) {
277285
ERR_clear_error();
278286
Nan::ThrowError("unable to find specified public key algorithm name.");
@@ -290,9 +298,18 @@ Local<Value> try_parse(const std::string& dataString) {
290298
char *rsa_e_dec, *rsa_n_hex;
291299
uint32_t rsa_key_length_int;
292300
RSA *rsa_key;
301+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
302+
rsa_key = EVP_PKEY_get1_RSA(pkey);
303+
const BIGNUM *n;
304+
const BIGNUM *e;
305+
RSA_get0_key(rsa_key, &n, &e, NULL);
306+
rsa_e_dec = BN_bn2dec(e);
307+
rsa_n_hex = BN_bn2hex(n);
308+
#else
293309
rsa_key = pkey->pkey.rsa;
294310
rsa_e_dec = BN_bn2dec(rsa_key->e);
295311
rsa_n_hex = BN_bn2hex(rsa_key->n);
312+
#endif
296313
rsa_key_length_int = RSA_size(rsa_key) * 8;
297314
Nan::Set(publicKey,
298315
Nan::New<String>("e").ToLocalChecked(),
@@ -322,7 +339,12 @@ Local<Value> try_parse(const std::string& dataString) {
322339
GENERAL_NAME *current = sk_GENERAL_NAME_value(names, i);
323340

324341
if (current->type == GEN_DNS) {
325-
char *name = (char*) ASN1_STRING_data(current->d.dNSName);
342+
char *name = NULL;
343+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
344+
name = (char *)ASN1_STRING_get0_data(current->d.dNSName);
345+
#else
346+
name = (char *)ASN1_STRING_data(current->d.dNSName);
347+
#endif
326348

327349
if (ASN1_STRING_length(current->d.dNSName) != (int) strlen(name)) {
328350
ERR_clear_error();
@@ -340,7 +362,11 @@ Local<Value> try_parse(const std::string& dataString) {
340362

341363
// Extensions
342364
Local<Object> extensions(Nan::New<Object>());
365+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
366+
const STACK_OF(X509_EXTENSION) *exts = X509_get0_extensions(cert);
367+
#else
343368
STACK_OF(X509_EXTENSION) *exts = cert->cert_info->extensions;
369+
#endif
344370
int num_of_exts;
345371
int index_of_exts;
346372
if (exts) {
@@ -360,7 +386,11 @@ Local<Value> try_parse(const std::string& dataString) {
360386
BIO *ext_bio = BIO_new(BIO_s_mem());
361387
// IFNULL_FAIL(ext_bio, "unable to allocate memory for extension value BIO");
362388
if (!X509V3_EXT_print(ext_bio, ext, 0, 0)) {
363-
M_ASN1_OCTET_STRING_print(ext_bio, ext->value);
389+
unsigned char *buf = NULL;
390+
int len = i2d_ASN1_OCTET_STRING(X509_EXTENSION_get_data(ext), &buf);
391+
if (len >= 0) {
392+
BIO_write(ext_bio, static_cast<const void *>(buf), len);
393+
}
364394
}
365395

366396
BUF_MEM *bptr;
@@ -430,21 +460,27 @@ Local<Value> parse_date(ASN1_TIME *date) {
430460
Local<Object> global = Nan::GetCurrentContext()->Global();
431461
Local<Object> DateObject = Nan::Get(global,
432462
Nan::New<String>("Date").ToLocalChecked()).ToLocalChecked()->ToObject();
433-
return scope.Escape(DateObject->CallAsConstructor(1, args));
463+
return scope.Escape(Nan::CallAsConstructor(DateObject, 1, args).ToLocalChecked());
434464
}
435465

436466
Local<Object> parse_name(X509_NAME *subject) {
437467
Nan::EscapableHandleScope scope;
438468
Local<Object> cert = Nan::New<Object>();
439469
int i, length;
440470
ASN1_OBJECT *entry;
441-
unsigned char *value;
471+
const unsigned char *value;
442472
char buf[255];
443473
length = X509_NAME_entry_count(subject);
444474
for (i = 0; i < length; i++) {
445475
entry = X509_NAME_ENTRY_get_object(X509_NAME_get_entry(subject, i));
446476
OBJ_obj2txt(buf, 255, entry, 0);
447-
value = ASN1_STRING_data(X509_NAME_ENTRY_get_data(X509_NAME_get_entry(subject, i)));
477+
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
478+
value = ASN1_STRING_get0_data(
479+
X509_NAME_ENTRY_get_data(X509_NAME_get_entry(subject, i)));
480+
#else
481+
value = ASN1_STRING_data(
482+
X509_NAME_ENTRY_get_data(X509_NAME_get_entry(subject, i)));
483+
#endif
448484
Nan::Set(cert,
449485
Nan::New<String>(real_name(buf)).ToLocalChecked(),
450486
Nan::New<String>((const char*) value).ToLocalChecked());

0 commit comments

Comments
 (0)