通过读取p12和pfx文件来伪造错误

时间:2014-09-25 00:21:54

标签: javascript pkcs#12 forge

尝试读取/解码p12和pfx文件时出现以下错误:

Cannot read PKCS#12 PFX. ASN.1 object is not an PKCS#12 PFX

Too few bytes to read ASN.1 value.

我正在尝试使用以下内容在Javascript中读取该文件:

<input id="cert-file" type="file" name="cert" /><output id="p12cert"></output>

使用JQuery,我附上一个&#34; on&#34;事件处理程序,用于检查所选文件。

$j("#cert-file").change(handleFileSelect);

function handleFileSelect(evt) {
    var files = evt.target.files; // FileList object
    getFile(files[0]);    
}

然后我尝试读取文件并使用伪造解码它。

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);

}

我不确定我是否只是错误地阅读了该文件。我离开here的FileReader示例。我做错了什么或者我的证书可能出错?

2 个答案:

答案 0 :(得分:7)

更新:在将数据传递给forge之前,问题似乎正在发生。数据未以正确的格式读取。您可以尝试使用以下选项之一:

选项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]);

选项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;

选项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));

选项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));

<强>旧

您是否尝试在非严格模式下加载PKCS#12?这通常会解决此特定错误:

var p12Asn1 = forge.asn1.fromDer(p12Der, false);

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

答案 1 :(得分:0)

这将完美地运作

  // 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);