我正在使用forge库创建.p12格式的自签名证书,该证书使用WebCryptoAPI生成私钥 - 公钥对。但是当我尝试在Windows证书库中导入.p12文件时,我收到以下错误:
此link表示私钥可能存在问题。
以下是webcryptoApi的密钥生成片段
window.crypto.subtle.generateKey({
name: 'RSA-PSS',
modulusLength: 2048,
publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
hash: {name: 'SHA-1'}
}
我生成p12的伪造代码段如下:
var newPkcs12Asn1 = forge.pkcs12.toPkcs12Asn1(
keys.privateKey, [cert], password,
{generateLocalKeyId: true, friendlyName: 'test'},
{algorithm: '3des'});
var newPkcs12Der = forge.asn1.toDer(newPkcs12Asn1).getBytes();
var p12b64 = forge.util.encode64(newPkcs12Der);
var downloadLink = document.createElement("a");
downloadLink.download = "example.p12";
downloadLink.innerHTML = "Download File";
downloadLink.setAttribute('href', 'data:application/x-pkcs12;base64,' + p12b64);
downloadLink.style.display = "none";
downloadLink.click();
注意:
答案 0 :(得分:1)
如评论中所示,问题是pkcs12编码参数中的语法错误
{generateLocalKeyId: true, friendlyName: 'test',algorithm: '3des'}
需要设置algorithm: '3des'
,因为默认情况下伪造会使用aes-128加密p12。
可以在此article中读取标准化PKCS#12的RFC7292,并未指定支持AES的必要性,但有足够的信息以可互操作的方式使用它。 Windows(甚至是windows10)无法处理使用更安全的加密方案和密码生成的文件。然后,可以使用的最安全的算法是triple-des