Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PKCS12 to PEM #576

Open
niyazagtc opened this issue Apr 10, 2018 · 5 comments
Open

PKCS12 to PEM #576

niyazagtc opened this issue Apr 10, 2018 · 5 comments

Comments

@niyazagtc
Copy link

niyazagtc commented Apr 10, 2018

I need to convert pkcs12 to pem. The pkcs12 is in ECC algorithm type.
I am trying with the below code

module.exports = function p12ToPem(p12Buffer, password) {
  if (Buffer.isBuffer(p12Buffer)) {
    p12Buffer = p12Buffer.toString("base64");
  }
  else if (typeof p12Buffer !== "string") {
    throw "p12ToPem only takes strings and buffers.";
  }

  var p12Der = forge.util.decode64(p12Buffer);
  var p12Asn1 = forge.asn1.fromDer(p12Der);
  var p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, true, password);
  var keyData = p12.getBags({ bagType: forge.pki.oids.keyBag });
  var certBags = p12.getBags({ bagType: forge.pki.oids.certBag })[forge.pki.oids.certBag];
  console.log('keyData ', keyData);
  console.log('certBags ', certBags);
  var key = forge.pki.privateKeyToPem(keyData);
  return key.replace(/\r\n/g, '\n');
};

The private key is not getting generated, it gives the below error,

Output:

keyData  { '1.2.840.113549.1.12.10.1.1': [] }
certBags  [ { type: '1.2.840.113549.1.12.10.1.3',
    attributes: { friendlyName: [Array], localKeyId: [Array] },
    cert: null,
    asn1:
     { tagClass: 0,
       type: 16,
       constructed: true,
       composed: true,
       value: [Array] } } ]
C:\workspace\AngularJSNodeJSDemo\node_modules\node-forge\js\rsa.js:1680
  var hex = b.toString(16);
              ^

TypeError: Cannot read property 'toString' of undefined
    at _bnToBytes (C:\workspace\AngularJSNodeJSDemo\node_modules\node-forge\js\rsa.js:1680:15)
    at Object.pki.privateKeyToAsn1.pki.privateKeyToRSAPrivateKey (C:\workspace\AngularJSNodeJSDemo\node_modules\node-forge\js\rsa.js:1286:7

Please advice whats wrong in this.

@dlongley
Copy link
Member

Unfortunately, we haven't yet implemented ASN.1 parsing for ECC keys, only RSA keys. Forge has an implementation for ed25519 but using only the raw (much simpler) format. PRs are welcome!

@armandodlvr
Copy link

Is this the same error?

TypeError: Cannot read property 'tagClass' of null
at Object.asn1.toDer (/Users/armandodelavegaruiz/raudo-api/node_modules/node-forge/lib/asn1.js:641:16)
at Object.pki.privateKeyInfoToPem (/Users/armandodelavegaruiz/raudo-api/node_modules/node-forge/lib/pki.js:99:16)

@sureshreddygovindu
Copy link

@bfricka @dlongley still not support ECC keytypes?

@lylest
Copy link

lylest commented Nov 29, 2020

TypeError: Cannot read property 'replace' of undefined

@sibelius
Copy link

is this fixed?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants