Created
January 12, 2018 08:21
-
-
Save variux/6b8ccde8d71a80d2b1575469218a9ce6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@variux You forgot "setEngine" call: