如何在 xadesjs 中签署文档?

How to sign a document in xadesjs?

我有一个关于数字签名和 xadesjs 的问题。我正在 Node.js 中编写一个小型服务器,它应该使用 XAdES 加密 XML 文件。我有一个 PFX 文件,我将其导出为 PEM 和 PK8 格式。一般问题是,当我使用 xadesjs 生成密钥对时,一切正常。这是一个例子:

// Generate RSA key pair
let privateKey, publicKey;
XAdES.Application.crypto.subtle.generateKey(
    {
        name: "RSASSA-PKCS1-v1_5",
        modulusLength: 1024, //can be 1024, 2048, or 4096,
        publicExponent: new Uint8Array([1, 0, 1]),
        hash: { name: "SHA-256" }, //can be "SHA-1", "SHA-256", "SHA-384", or "SHA-512"
    },
    false, //whether the key is extractable (i.e. can be used in exportKey)
    ["sign", "verify"] //can be any combination of "sign" and "verify"
)
.then(function (keyPair) {
    privateKey = keyPair.privateKey;

    // Call sign function
    return SignXml(xmlString, privateKey, 
        { name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-256" } });
})
.then(function (signedDocument) {
    console.log("Signed document:\n\n", signedDocument);
    next(null, signedDocument);
})
.catch(function (e) {
    console.log(e);
    next(e, null);
});

但我一直在使用可能所有可能的组合来使 importKey 方法起作用。例如,这不起作用,即使密钥在 PKCS8 中(使用 OpenSSL 导出):

let key =  fs.readFileSync("key.pem");

XAdES.Application.crypto.subtle.importKey("pkcs8", key,
    {
        name: "RSASSA-PKCS1-v1_5",
        modulusLength: 2048, //can be 1024, 2048, or 4096,
        publicExponent: new Uint8Array([1, 0, 1]),
        hash: { name: "SHA-256" },
    },
    false, 
    ["sign"]
)
.then(function (privateKey) {
    // Call sign function
    return SignXml(xmlString, privateKey, 
         { name: "RSASSA-PKCS1-v1_5", hash: { name: "SHA-256" } });
})

我得到一个错误:

Error: ImportKey: Can not import key for pkcs8

我的主要目标是获得一个程序,我可以在其中指定数字签名的路径,我的程序将导入密钥和证书,最后用它们对我的文件进行签名。如果所有的东西都可以存储在一个PFX文件中就好了,但是如果你有任何解决方案,即使密钥和证书分开存储(PEM和PK8),我将不胜感激。

万一有人遇到同样的问题。这是在@pedrofb 的回答的帮助下实现的。

首先,我在 pem npm 包的帮助下获得了密钥和证书。然后我删除页眉和页脚并将键转换为 ArrayBuffer:

const pfx = fs.readFileSync("cert.pfx");
pem.readPkcs12(pfx, { p12Password: "test123" }, (err: any, cert: any) => {
    if(err) return console.log(err);
    let privateKey = b64ToBinary(removePFXComments(cert.key));
    let certificate = removePFXComments(cert.cert);

下面是上面两个使用方法的实现:

function removePFXComments(pem) {
    let lines = pem.split('\n');
    let encoded = '';
    for (let i = 0; i < lines.length; i++) {
        if (lines[i].trim().length > 0 &&
            lines[i].indexOf('-----BEGIN CERTIFICATE-----') < 0 &&
            lines[i].indexOf('-----END CERTIFICATE') < 0 &&
            lines[i].indexOf('-----BEGIN RSA PRIVATE KEY-----') < 0 &&
            lines[i].indexOf('-----BEGIN RSA PUBLIC KEY-----') < 0 &&
            lines[i].indexOf('-----BEGIN PUBLIC KEY-----') < 0 &&
            lines[i].indexOf('-----END PUBLIC KEY-----') < 0 &&
            lines[i].indexOf('-----BEGIN PRIVATE KEY-----') < 0 &&
            lines[i].indexOf('-----END PRIVATE KEY-----') < 0 &&
            lines[i].indexOf('-----END RSA PRIVATE KEY-----') < 0 &&
            lines[i].indexOf('-----END RSA PUBLIC KEY-----') < 0) {
            encoded += lines[i].trim();
        }
    }
    return encoded;
}

function b64ToBinary(base64) {
  let raw = atob(base64);
  let rawLength = raw.length;
  let array = new Uint8Array(new ArrayBuffer(rawLength));

  for(let i = 0; i < rawLength; i++) {
    array[i] = raw.charCodeAt(i);
  }
  return array;
}