Thursday, 1 June 2017

How to imoprt pem and p12 files in node.js for sending push-notification?

I am trying to use these two files but somehow it throws an error

My code of snippet

var forge = require('node-forge');
var fs = require('fs');

var keyFile = fs.readFileSync("./APN.pem", 'binary');
// var keyFile = fs.readFileSync("./Certificates.p12", 'binary');
var p12Asn1 = forge.asn1.fromDer(keyFile);

var p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, '123456');

var bags = p12.getBags({bagType: forge.pki.oids.certBag});

var bag = bags[forge.pki.oids.certBag][0];

// convert to ASN.1, then DER, then PEM-encode
var msg = {
  type: 'CERTIFICATE',
  body: forge.asn1.toDer(bag.asn1).getBytes()
};
var pem = forge.pem.encode(msg);

console.log(pem);

But It throws error fs.js:584 return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);

Also I see in the link Node JS, how to extract X.509 Certificate from P12 file? , they are opening the file and the same code now I am using and not able to do so. Also the path to the file is correct as other files I am importing with the same path in this file

Thanks in advance



via VIKAS KOHLI

No comments:

Post a Comment