2

我正在使用forge库创建一个 .p12 格式的自签名证书,该证书使用WebCryptoAPI生成私钥-公钥对。但是,当我尝试在 Windows 证书存储中导入 .p12 文件时,出现以下错误:

在此处输入图像描述

这个链接说私钥可能有问题。

以下是我通过 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();

笔记 :

  • 我也无法在 Mozilla 证书存储中导入文件。那么p12文件可能有问题吗?
  • Windows 证书存储在导入时正确验证我的私钥密码,仅完成阶段失败。
4

1 回答 1

1

如评论所示,问题是pkcs12编码参数中的语法错误

 {generateLocalKeyId: true, friendlyName: 'test',algorithm: '3des'}

需要设置algorithm: '3des',因为forge默认使用aes-128加密p12。

正如可以在本文中看到的那样,标准化 PKCS#12 的 RFC7292 没有指定需要支持 AES,但是有足够的信息可以以可互操作的方式使用它。Windows(甚至 windows10)无法处理使用更安全的加密方案和密码生成的文件。那么,可以使用的最安全的算法是triple-des

于 2017-03-28T16:59:45.710 回答