Node JS, how to extract X.509 Certificate from P12 file?
<p>I have <code>p12</code> file, where I should get X.509 Certificate. In order to work with this file I use <code>forge</code> library:</p>

<pre><code>var forge = require('node-forge');
var fs = require('fs');

var keyFile = fs.readFileSync("/path/to/p12/file.p12", 'binary');
var p12Asn1 = forge.asn1.fromDer(keyFile);

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

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

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

console.log(cert);
</code></pre>

<p>Console outputs to me this kind of information:</p>

<pre><code>{ type: '1.2.840.113549.1.12.10.1.3',
  attributes:
  { localKeyId: [ 'aoa ??xx\u0015-?]%m§ §\f,\u0013' ],
    friendlyName: [ 'e56fe5a0899f787815adaf5d256da7a0a70c2c13' ] },
    cert: null,
    asn1:
    { tagClass: 0,
      type: 16,
      constructed: true,
      composed: true,
      value: [ [Object], [Object], [Object] ] } }
</code></pre>

<p>This result means that I have an alias with name <code>e56fe5a0899f787815adaf5d256da7a0a70c2c13</code>, but why <code>cert</code> is <code>null</code>?</p>

<p>There is Java's security api's, which is able to extract X.509 certificate from this p12 file by it's alias.</p>

<pre><code>X509Certificate x509Certificate = (X509Certificate) ks.getCertificate(alias);
</code></pre>

<p>How it is possible to extract X.509 certificate from <code>p12</code> file by using <code>forge</code>?</p>

<p>Node version <code>5.4.1</code></p>

<p>Forge Version <code>0.6.45</code></p>

<p>There you can download my testing p12 file: <a href="https://drive.google.com/open?id=0B8Xi-qhe93I8QjYzcndoTmhFazQ" rel="nofollow noreferrer">link</a></p>

<p>password is <code>123456</code></p>