admin管理员组文章数量:1287840
I am having the following errors when trying read/decode p12 and pfx files:
Cannot read PKCS#12 PFX. ASN.1 object is not an PKCS#12 PFX
Too few bytes to read ASN.1 value.
I am trying to read the file in Javascript with the following:
<input id="cert-file" type="file" name="cert" /><output id="p12cert"></output>
Using JQuery, I attach a "on change" event handler, to check the selected file.
$j("#cert-file").change(handleFileSelect);
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
getFile(files[0]);
}
Then I try to read the file and decode it using forge.
function getFile(p12cert)
{
var reader = new FileReader();
var password = 'password';
reader.onload = (function (theFile) {
return function(eve) {
var p12Der = forge.util.decode64(eve.target.result);
// get p12 as ASN.1 object
// Not working for one of my p12 files
var p12Asn1 = forge.asn1.fromDer(p12Der);
// decrypt p12 using the password 'password'
// TODO: Not working for some reason for p12 and pfx file
var p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, password);
};
})(p12cert);
reader.readAsText(p12cert);
}
I'm not sure if I'm just reading the file in wrong. I was going off of the FileReader examples from here. Am I doing something wrong or could something possibly be wrong with my certs?
I am having the following errors when trying read/decode p12 and pfx files:
Cannot read PKCS#12 PFX. ASN.1 object is not an PKCS#12 PFX
Too few bytes to read ASN.1 value.
I am trying to read the file in Javascript with the following:
<input id="cert-file" type="file" name="cert" /><output id="p12cert"></output>
Using JQuery, I attach a "on change" event handler, to check the selected file.
$j("#cert-file").change(handleFileSelect);
function handleFileSelect(evt) {
var files = evt.target.files; // FileList object
getFile(files[0]);
}
Then I try to read the file and decode it using forge.
function getFile(p12cert)
{
var reader = new FileReader();
var password = 'password';
reader.onload = (function (theFile) {
return function(eve) {
var p12Der = forge.util.decode64(eve.target.result);
// get p12 as ASN.1 object
// Not working for one of my p12 files
var p12Asn1 = forge.asn1.fromDer(p12Der);
// decrypt p12 using the password 'password'
// TODO: Not working for some reason for p12 and pfx file
var p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, password);
};
})(p12cert);
reader.readAsText(p12cert);
}
I'm not sure if I'm just reading the file in wrong. I was going off of the FileReader examples from here. Am I doing something wrong or could something possibly be wrong with my certs?
Share Improve this question asked Sep 25, 2014 at 0:21 RavenBellVanessaRavenBellVanessa 831 silver badge5 bronze badges2 Answers
Reset to default 7Update: It looks like the problem is occurring before the data is passed to forge. The data isn't being read in the proper format. You can try one of these options instead:
Option 1:
reader.readAsDataURL(p12cert); // change from readAsText
// in reader.onload, parse out the base64 part:
var p12Der = forge.util.decode64(eve.target.result.split(',')[1]);
Option 2:
reader.readAsBinaryString(p12cert); // change from readAsText
// in reader.onload, skip base64 decoding step entirely since the data is
// already in a binary string that forge can work with -- the downside
// is that this method is deprecated in the FileReader API
var p12Der = eve.target.result;
Option 3:
// instead, use an ArrayBuffer
reader.readAsArrayBuffer(p12cert);
// in reader.onload, convert to base64 and then decode as you were doing before
var b64 = forge.util.binary.base64.encode(new Uint8Array(eve.target.result));
Option 4:
// instead, use an ArrayBuffer
reader.readAsArrayBuffer(p12cert);
// in reader.onload, just do a raw conversion to a binary string and skip
// the base64 decoding (though this may cause a stack overflow
// with the current implementation in forge which is experimental)
var p12Der = forge.util.binary.raw.encode(new Uint8Array(eve.target.result));
Old:
Have you tried loading the PKCS#12 in non-strict mode? This will often resolve this particular error:
var p12Asn1 = forge.asn1.fromDer(p12Der, false);
var p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, false, password);
This will work perfectly
// get p12 as ASN.1 object
//here buffer is a result for readFileSync pkcs12 file
var p12Asn1 = forge.asn1.fromDer(buffer);
// decrypt p12 using the password 'password'
var p12 = forge.pkcs12.pkcs12FromAsn1(p12Asn1, password);
// get bags by type
var certBags = p12.getBags({bagType: forge.pki.oids.certBag});
var pkeyBags = p12.getBags({bagType: forge.pki.oids.pkcs8ShroudedKeyBag});
// fetching certBag
var certBag = certBags[forge.pki.oids.certBag][0];
// fetching keyBag
var keybag = pkeyBags[forge.pki.oids.pkcs8ShroudedKeyBag][0];
// generate pem from private key
var privateKeyPem = forge.pki.privateKeyToPem(keybag.key);
// generate pem from cert
var certificate = forge.pki.certificateToPem(certBag.cert);
本文标签: javascriptForge Errors with reading p12 and pfx filesStack Overflow
版权声明:本文标题:javascript - Forge Errors with reading p12 and pfx files - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741310065a2371599.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论