Skip to content

Instantly share code, notes, and snippets.

@variux
Created January 12, 2018 08:21
Show Gist options
  • Save variux/6b8ccde8d71a80d2b1575469218a9ce6 to your computer and use it in GitHub Desktop.
Save variux/6b8ccde8d71a80d2b1575469218a9ce6 to your computer and use it in GitHub Desktop.
const WebCrypto = require("node-webcrypto-ossl");
const crypto = new WebCrypto();
const fs = require('fs');
const asn1js = require('asn1js');
const pkijs = require('pkijs');
const pvutils = require('pvutils');
function parsePKCS12Internal(buffer, password)
{
//region Initial variables
let sequence = Promise.resolve();
const passwordConverted = pvutils.stringToArrayBuffer(password);
//endregion
//region Parse internal PKCS#12 values
const asn1 = asn1js.fromBER(buffer);
const pkcs12 = new pkijs.PFX({ schema: asn1.result });
//endregion
//region Parse "AuthenticatedSafe" value of PKCS#12 data
sequence = sequence.then(
() => pkcs12.parseInternalValues({
password: passwordConverted,
checkIntegrity: false
})
);
//endregion
//region Parse "SafeContents" values
sequence = sequence.then(
() => pkcs12.parsedValue.authenticatedSafe.parseInternalValues({
safeContents: [
{
// Empty parameters since for first "SafeContent" OpenSSL uses "no privacy" protection mode
},
{
password: passwordConverted
}
]
})
);
//endregion
//region Parse "PKCS8ShroudedKeyBag" value
sequence = sequence.then(
() => pkcs12.parsedValue.authenticatedSafe.parsedValue.safeContents[0].value.safeBags[0].bagValue.parseInternalValues({
password: passwordConverted
})
);
//endregion
//region Store parsed value to Web page
sequence = sequence.then(
() => pkcs12
);
//endregion
return sequence;
}
var file = new Uint8Array(fs.readFileSync("firmaultima.p12")).buffer;
console.log(parsePKCS12Internal(file, 2002));
@YuryStrozhevsky
Copy link

@variux You forgot "setEngine" call:

const WebCrypto = require("node-webcrypto-ossl");
const webcrypto = new WebCrypto();
setEngine("newEngine", webcrypto, new CryptoEngine({ name: "", crypto: webcrypto, subtle: webcrypto.subtle }));

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