mercredi 4 mars 2015

Custom iojs addon failed to load RSA private key


The code snippet that I try to load an RSA private key in iojs addon.



void IojsAddon::New(const FunctionCallbackInfo<Value>& args) {
Isolate* isolate = Isolate::GetCurrent();
HandleScope scope(isolate);

if (args.IsConstructCall()) {
EVP_PKEY* ca_key;
BIO* keyb = BIO_new(BIO_s_file());
BIO_read_filename(keyb, "/path/to/key.pem");
ca_key = PEM_read_bio_PrivateKey(keyb, nullptr, nullptr, nullptr);

std::cout << "type: " << ca_key->type << std::endl; // type: 6
std::cout << "check: " << RSA_check_key(ca_key->pkey.rsa) << std::endl; // check: -1
RSA_print_fp(stdout, ca_key->pkey.rsa, 0); // Segmentation fault: 11

IojsAddon* addon = new IojsAddon();
addon->Wrap(args.This());
args.GetReturnValue().Set(args.This());
} else {
const int argc = 1;
Local<Value> argv[argc] = { args[0] };
Local<Function> cons = Local<Function>::New(isolate, constructor);
args.GetReturnValue().Set(cons->NewInstance(argc, argv));
}
}


The code snippet that load an RSA private key successfully in a standalone application which exactly the same as the previous.



int main(int argc, char **argv) {
EVP_PKEY* ca_key;
BIO* keyb = BIO_new(BIO_s_file());
BIO_read_filename(keyb, "/path/to/key.pem");
ca_key = PEM_read_bio_PrivateKey(keyb, nullptr, nullptr, nullptr);

std::cout << "type: " << ca_key->type << std::endl; // type: 6
std::cout << "check: " << RSA_check_key(ca_key->pkey.rsa) << std::endl; // check: -1
RSA_print_fp(stdout, ca_key->pkey.rsa, 0); // print out the key info successfully.
return (0);
}


P.S.



  • iojs: v1.2.0

  • pangyp: v2.0.1


What did I miss here? Any help would be appreciate!




Aucun commentaire:

Enregistrer un commentaire